22 #ifndef __D_MESH_EFFECT_H__
23 #define __D_MESH_EFFECT_H__
25 #include "dCoreStdafx.h"
28 #include "dPolyhedra.h"
31 #define DG_MESH_EFFECT_PRECISION_BITS 48
32 #define DG_MESH_EFFECT_PRECISION_SCALE dFloat64(dInt64(1)<<DG_MESH_EFFECT_PRECISION_BITS)
33 #define DG_MESH_EFFECT_PRECISION_SCALE_INV (dFloat64 (1.0f) / DG_MESH_EFFECT_PRECISION_SCALE)
35 #define DG_VERTEXLIST_INDEXLIST_TOL (dFloat64 (0.0f))
36 #define DG_MESH_EFFECT_POINT_SPLITED 512
37 #define DG_MESH_EFFECT_BVH_STACK_DEPTH 256
42 dInt32 m_materialCount;
44 dInt32 m_materials[256];
45 dInt32 m_materialsIndexCount[256];
59 dgMeshBVHNode (
const dMeshEffect*
const mesh,
dEdge*
const face,
void*
const userData);
60 dgMeshBVHNode (dgMeshBVHNode*
const left, dgMeshBVHNode*
const right);
64 DG_CLASS_ALLOCATOR(allocator)
71 dgMeshBVHNode* m_left;
72 dgMeshBVHNode* m_right;
73 dgMeshBVHNode* m_parent;
76 class dgFitnessList:
public dTree <dgMeshBVHNode*, dgMeshBVHNode*>
79 dgFitnessList (dMemoryAllocator___*
const allocator);
80 dFloat64 TotalCost ()
const;
87 virtual void Build ();
88 virtual void Cleanup ();
94 virtual dgMeshBVHNode* CreateLeafNode (
dEdge*
const face,
void*
const userData) = 0;
96 dgMeshBVHNode* AddFaceNode (
dEdge*
const face,
void*
const userData);
97 void RemoveNode (dgMeshBVHNode*
const treeNode);
98 void ImproveNodeFitness ();
99 void ImproveNodeFitness (dgMeshBVHNode*
const node);
100 dFloat32 CalculateSurfaceArea (dgMeshBVHNode*
const node0, dgMeshBVHNode*
const node1,
dVector& minBox,
dVector& maxBox)
const;
101 virtual bool SanityCheck()
const;
103 virtual dFloat64 RayFaceIntersect (
const dgMeshBVHNode*
const face,
const dBigVector& p0,
const dBigVector& p1,
void*
const userData)
const;
108 dgMeshBVHNode* m_rootNode;
109 dgFitnessList m_fitness;
118 dMeshEffect (dMemoryAllocator___*
const allocator, dgDeserialize deserialization,
void*
const userData);
121 dMeshEffect(dMemoryAllocator___*
const allocator,
const char*
const fileName);
124 dMeshEffect (dMemoryAllocator___*
const allocator,
const dFloat64*
const vertexCloud, dInt32 count, dInt32 strideInByte, dFloat64 distTol);
127 dMeshEffect(dMemoryAllocator___*
const allocator,
const dMatrix& planeMatrix, dFloat32 witdth, dFloat32 breadth, dInt32 material,
const dMatrix& textureMatrix0,
const dMatrix& textureMatrix1);
131 void ApplyTransform (
const dMatrix& matrix);
136 void CylindricalMapping (dInt32 cylinderMaterial, dInt32 capMaterial,
const dMatrix& uvAligment);
137 void AngleBaseFlatteningMapping (dInt32 cylinderMaterial, dgReportProgress progressReportCallback,
void*
const userData);
139 dEdge* InsertEdgeVertex (
dEdge*
const edge, dFloat64 param);
154 void ConvertToPolygons ();
155 void RemoveUnusedVertices(dInt32*
const vertexRemapTable);
158 dInt32 GetVertexCount()
const;
159 dInt32 GetVertexStrideInByte()
const;
160 const dFloat64* GetVertexPool ()
const;
162 dInt32 GetVertexBaseCount()
const;
163 void SetVertexBaseCount(dInt32 count);
165 dEdge* SpliteFace (dInt32 v0, dInt32 v1);
167 dInt32 GetTotalFaceCount()
const;
168 dInt32 GetTotalIndexCount()
const;
169 void GetFaces (dInt32*
const faceCount, dInt32*
const materials,
void**
const faceNodeList)
const;
171 bool HasOpenEdges ()
const;
173 dFloat64 CalculateVolume ()
const;
175 void OptimizePoints();
176 void OptimizeAttibutes();
177 const dInt32* GetIndexToVertexMap()
const;
179 bool HasLayersChannel()
const;
180 bool HasNormalChannel()
const;
181 bool HasBinormalChannel()
const;
182 bool HasUV0Channel()
const;
183 bool HasUV1Channel()
const;
184 bool HasVertexColorChannel()
const;
186 dgCollisionInstance* CreateCollisionTree(dgWorld*
const world, dInt32 shapeID)
const;
187 dgCollisionInstance* CreateConvexCollision(dgWorld*
const world, dFloat64 tolerance, dInt32 shapeID,
const dMatrix& matrix = dGetIdentityMatrix())
const;
189 dMeshEffect* CreateSimplification (dInt32 maxVertexCount, dgReportProgress reportProgressCallback,
void*
const userData)
const;
190 dMeshEffect* CreateConvexApproximation (dFloat32 maxConcavity, dFloat32 backFaceDistanceFactor, dInt32 maxHullOuputCount, dInt32 maxVertexPerHull, dgReportProgress reportProgressCallback,
void*
const userData)
const;
193 void CreateTetrahedraLinearBlendSkinWeightsChannel (
const dMeshEffect*
const tetrahedraMesh);
195 static dMeshEffect* CreateVoronoiConvexDecomposition (dMemoryAllocator___*
const allocator, dInt32 pointCount, dInt32 pointStrideInBytes,
const dFloat32*
const pointCloud, dInt32 materialId,
const dMatrix& textureProjectionMatrix);
196 static dMeshEffect* CreateFromSerialization (dMemoryAllocator___*
const allocator, dgDeserialize deserialization,
void*
const userData);
198 void LoadOffMesh (
const char*
const filename);
199 void LoadTetraMesh (
const char*
const filename);
200 void Serialize (dgSerialize callback,
void*
const userData)
const;
203 dInt32 GetVertexLayer (dInt32 index)
const;
205 void TransformMesh (
const dMatrix& matrix);
207 void* GetFirstVertex ()
const;
208 void* GetNextVertex (
const void*
const vertex)
const;
209 int GetVertexIndex (
const void*
const vertex)
const;
211 void* GetFirstPoint ()
const;
212 void* GetNextPoint (
const void*
const point)
const;
213 dInt32 GetPointIndex (
const void*
const point)
const;
214 dInt32 GetVertexIndexFromPoint (
const void*
const point)
const;
216 void* GetFirstEdge ()
const;
217 void* GetNextEdge (
const void*
const edge)
const;
219 void GetEdgeIndex (
const void*
const edge, dInt32& v0, dInt32& v1)
const;
222 const dEdge* GetPolyhedraEdgeFromNode(
const void*
const edge)
const;
224 void* GetFirstFace ()
const;
225 void* GetNextFace (
const void*
const face)
const;
226 dInt32 IsFaceOpen (
const void*
const face)
const;
227 dInt32 GetFaceMaterial (
const void*
const face)
const;
228 dInt32 GetFaceIndexCount (
const void*
const face)
const;
229 void GetFaceIndex (
const void*
const face, dInt32*
const indices)
const;
230 void GetFaceAttributeIndex (
const void*
const face, dInt32*
const indices)
const;
231 dBigVector CalculateFaceNormal (
const void*
const face)
const;
233 void SetFaceMaterial (
const void*
const face,
int materialID);
234 void AddInterpolatedEdgeAttribute (
dEdge*
const edge, dFloat64 param);
235 dInt32 InterpolateVertex (
const dBigVector& point,
const dEdge*
const face)
const;
240 dInt32 CalculateMaxAttributes ()
const;
241 dFloat64 QuantizeCordinade(dFloat64 val)
const;
249 dMeshEffect* CreateVoronoiConvex (
const dBigVector*
const conevexPointCloud, dInt32 count, dInt32 materialId,
const dMatrix& textureProjectionMatrix, dFloat32 normalAngleInRadians)
const;
251 void PackPoints (dFloat64 tol);
255 friend class dgConvexHull4d;
256 friend class dgBooleanMeshBVH;
257 friend class dgHACDClusterGraph;
258 friend class dgTriangleAnglesToUV;
259 friend class dgTetraIsoSufaceStuffing;
260 friend class dgCollisionCompoundFractured;
276 template<
class T, dChannelType type>
277 class dChannel:
public dArray<T>
281 dChannel(
const dChannel& source);
285 void PushBack(
const T& element);
328 dInt32 m_vertexIndex;
329 dInt32 m_attibuteIndex;
334 const dChannel<dBigVector, m_point>* m_points;
335 dInt32 m_vertexSortIndex;
338 dInt32 GetSortIndex(
const dChannel<dBigVector, m_point>& points, dFloat64& dist)
const;
339 static dInt32 CompareVertex(
const dSortKey*
const ptr0,
const dSortKey*
const ptr1,
void*
const context);
342 class dPointFormat:
public dFormat
346 dPointFormat(
const dPointFormat& source);
350 void SetCount(dInt32 count);
351 void CompressData(dInt32*
const indexList);
353 dChannel<dInt32, m_layer> m_layers;
354 dChannel<dBigVector, m_point> m_vertex;
357 class dAttibutFormat:
public dFormat
368 dAttibutFormat(
const dAttibutFormat& source);
372 void SetCount(dInt32 count);
373 void CopyFrom(
const dAttibutFormat& source);
374 void CopyEntryFrom(dInt32 index,
const dAttibutFormat& source, dInt32 sourceIndex);
375 void CompressData(
const dPointFormat& points, dInt32*
const indexList);
377 dChannel<dInt32, m_vertex> m_pointChannel;
378 dChannel<dInt32, m_material> m_materialChannel;
379 dChannel<dTriplex, m_normal> m_normalChannel;
380 dChannel<dTriplex, m_binormal> m_binormalChannel;
381 dChannel<dVector, m_color> m_colorChannel;
382 dChannel<dgUV, m_uv0> m_uv0Channel;
383 dChannel<dgUV, m_uv1> m_uv1Channel;
393 const dFloat64* m_data;
394 const dInt32* m_indexList;
395 dInt32 m_strideInBytes;
401 const dFloat32* m_data;
402 const dInt32* m_indexList;
403 dInt32 m_strideInBytes;
417 const dInt32* m_faceIndexCount;
418 const dInt32* m_faceMaterial;
419 dDoubleData m_vertex;
421 dFloatData m_binormal;
424 dFloatData m_vertexColor;
431 D_CORE_API
void CalculateNormals(dFloat64 angleInRadians);
432 D_CORE_API
void BuildFromIndexList(
const dMeshVertexFormat*
const format);
434 dInt32 GetPropertiesCount()
const;
435 D_CORE_API
void GetVertexChannel64(dInt32 strideInByte, dFloat64*
const bufferOut)
const;
436 D_CORE_API
void GetVertexChannel(dInt32 strideInByte, dFloat32*
const bufferOut)
const;
437 D_CORE_API
void GetNormalChannel(dInt32 strideInByte, dFloat32*
const bufferOut)
const;
438 D_CORE_API
void GetBinormalChannel(dInt32 strideInByte, dFloat32*
const bufferOut)
const;
439 D_CORE_API
void GetUV0Channel(dInt32 strideInByte, dFloat32*
const bufferOut)
const;
440 D_CORE_API
void GetUV1Channel(dInt32 strideInByte, dFloat32*
const bufferOut)
const;
441 D_CORE_API
void GetVertexColorChannel(dInt32 strideInByte, dFloat32*
const bufferOut)
const;
446 D_CORE_API dInt32 GetFirstMaterial(
ndIndexArray*
const handle)
const;
447 D_CORE_API dInt32 GetNextMaterial(
ndIndexArray*
const handle, dInt32 materialHandle)
const;
448 D_CORE_API dInt32 GetMaterialID(
ndIndexArray*
const handle, dInt32 materialHandle)
const;
449 D_CORE_API dInt32 GetMaterialIndexCount(
ndIndexArray*
const handle, dInt32 materialHandle)
const;
450 D_CORE_API
void GetMaterialGetIndexStream(
ndIndexArray*
const handle, dInt32 materialHandle, dInt32*
const index)
const;
451 D_CORE_API
void GetMaterialGetIndexStreamShort(
ndIndexArray*
const handle, dInt32 materialHandle, dInt16*
const index)
const;
452 D_CORE_API
void MaterialGeomteryEnd(
ndIndexArray*
const handle);
454 D_CORE_API
void BeginBuild();
465 D_CORE_API
void EndBuild(dFloat64 tol,
bool fixTjoint =
true);
468 D_CORE_API
void SphericalMapping(dInt32 material,
const dMatrix& uvAligment);
469 D_CORE_API
void UniformBoxMapping(dInt32 material,
const dMatrix& textureMatrix);
470 D_CORE_API
void BoxMapping(dInt32 front, dInt32 side, dInt32 top,
const dMatrix& uvAligment);
473 D_CORE_API
void Init();
474 D_CORE_API
void RepairTJoints();
475 D_CORE_API
virtual void BeginFace();
476 D_CORE_API
virtual bool EndFace();
479 void PackAttibuteData();
480 void UnpackAttibuteData();
481 bool SeparateDuplicateLoops(
dEdge*
const face);
482 dInt32 AddInterpolatedHalfAttribute(
dEdge*
const edge, dInt32 midPoint);
484 dPointFormat m_points;
485 dAttibutFormat m_attrib;
486 dInt32 m_vertexBaseCount;
487 dInt32 m_constructionIndex;
491 inline dInt32 dMeshEffect::GetVertexCount()
const
493 return m_points.m_vertex.m_count;
496 inline dInt32 dMeshEffect::GetVertexBaseCount()
const
498 return m_vertexBaseCount;
501 inline void dMeshEffect::SetVertexBaseCount(dInt32 count)
503 m_vertexBaseCount = count;
507 inline const dInt32* dMeshEffect::GetIndexToVertexMap()
const
509 return &m_attrib.m_pointChannel[0];
512 inline dBigVector dMeshEffect::GetVertex (dInt32 index)
const
515 dAssert(index < m_points.m_vertex.m_count);
516 return m_points.m_vertex[index];
519 inline bool dMeshEffect::HasLayersChannel()
const
521 return m_points.m_layers.m_count != 0;
524 inline dInt32 dMeshEffect::GetVertexLayer(dInt32 index)
const
527 dAssert(index < m_points.m_vertex.m_count);
528 return (m_points.m_layers.m_count) ? m_points.m_layers[index] : 0;
532 inline dInt32 dMeshEffect::GetVertexStrideInByte()
const
537 inline const dFloat64* dMeshEffect::GetVertexPool ()
const
539 return &m_points.m_vertex[0].m_x;
544 return GetNextLayer (IncLRU());
552 return GetNextLayer (layerSegment->IncLRU() - 1);
556 inline dFloat64 dMeshEffect::QuantizeCordinade(dFloat64 x)
const
559 dFloat64 mantissa = frexp(x, &exp);
560 mantissa = DG_MESH_EFFECT_PRECISION_SCALE_INV * floor (mantissa * DG_MESH_EFFECT_PRECISION_SCALE);
562 dFloat64 x1 = ldexp(mantissa, exp);
567 template<
class T, dMeshEffect::dChannelType type>
568 dMeshEffect::dChannel<T, type>::dChannel()
575 template<
class T, dMeshEffect::dChannelType type>
576 dMeshEffect::dChannel<T, type>::dChannel(
const dChannel& source)
578 ,m_type(source.m_type)
579 ,m_isValid(source.m_isValid)
583 template<
class T, dMeshEffect::dChannelType type>
584 dMeshEffect::dChannel<T, type>::~dChannel()
588 template<
class T, dMeshEffect::dChannelType type>
589 void dMeshEffect::dChannel<T, type>::Clear()
595 template<
class T, dMeshEffect::dChannelType type>
596 void dMeshEffect::dChannel<T, type>::PushBack(
const T& element)
603 inline dMeshEffect::dPointFormat::dPointFormat()
609 inline dMeshEffect::dPointFormat::dPointFormat(
const dPointFormat& source)
610 :m_layers(source.m_layers)
611 , m_vertex(source.m_vertex)
615 inline dMeshEffect::dPointFormat::~dPointFormat()
619 inline void dMeshEffect::dPointFormat::Clear()
625 inline void dMeshEffect::dPointFormat::SetCount(dInt32 count)
627 m_layers.SetCount(count);
628 m_vertex.SetCount(count);
631 inline dMeshEffect::dAttibutFormat::dAttibutFormat()
642 inline dMeshEffect::dAttibutFormat::dAttibutFormat(
const dAttibutFormat& source)
643 :m_pointChannel(source.m_pointChannel)
644 ,m_materialChannel(source.m_materialChannel)
645 ,m_normalChannel(source.m_normalChannel)
646 ,m_binormalChannel(source.m_binormalChannel)
647 ,m_colorChannel(source.m_colorChannel)
648 ,m_uv0Channel(source.m_uv0Channel)
649 ,m_uv1Channel(source.m_uv1Channel)
653 inline dMeshEffect::dAttibutFormat::~dAttibutFormat()
657 inline void dMeshEffect::dAttibutFormat::Clear()
659 m_pointChannel.Clear();
660 m_materialChannel.Clear();
661 m_normalChannel.Clear();
662 m_binormalChannel.Clear();
663 m_colorChannel.Clear();
664 m_uv0Channel.Clear();
665 m_uv1Channel.Clear();
668 inline void dMeshEffect::dAttibutFormat::SetCount(dInt32 count)
670 m_pointChannel.SetCount(count);
671 m_materialChannel.SetCount(count);
672 m_normalChannel.SetCount(count);
673 m_binormalChannel.SetCount(count);
674 m_colorChannel.SetCount(count);
675 m_uv0Channel.SetCount(count);
676 m_uv1Channel.SetCount(count);
679 inline dInt32 dMeshEffect::GetPropertiesCount()
const
681 return m_attrib.m_pointChannel.GetCount();