26#include "../Triangle/triangle.h"
27#include "../Triangle/triangle_api.h"
33CMeshDomainPlane::CMeshDomainPlane(
double Seed,
bool bFillEnds)
46 vector<CMesh>::iterator itDomainMeshes;
48 vector<CMesh>::iterator itTriangulatedMeshes;
51 vector<XYZ> DomainMeshNormals;
53 vector<PLANEPARAMS> PlaneParams;
57 const list<int> &QuadIndices = itDomainMeshes->GetIndices(
CMesh::QUAD);
58 const list<int> &PolygonIndices = itDomainMeshes->GetIndices(
CMesh::POLYGON);
59 list<int>::const_iterator itQuadIndices;
60 list<int>::const_iterator itPolyIndices;
61 vector<int> NumVertices;
62 vector<XY> HolePoints;
63 bool bIsQuad = QuadIndices.size() > 0;
67 if (PolygonIndices.size() > 0)
69 for (itPolyIndices = PolygonIndices.begin(); itPolyIndices != PolygonIndices.end(); )
72 int Start = *itPolyIndices;
77 }
while ((itPolyIndices != PolygonIndices.end()) && ((*itPolyIndices) != Start));
79 if ((*itPolyIndices) != Start)
81 TGERROR(
"Error creating intersection of yarn with domain");
85 NumVertices.push_back(Num);
95 vector< vector<XY> > ArrayPoints2D;
102 PlaneParams.push_back(ConvertRef);
103 ArrayPoints2D.push_back(Points2D);
109 itPolyIndices = PolygonIndices.begin();
112 advance(itPolyIndices, NumVertices[0]);
116 for ( itPolyIndices; itPolyIndices != PolygonIndices.end(); )
118 vector<XYZ> Points3D;
119 for (
int iNode = 0; iNode < NumVertices[Poly] - 1; ++iNode)
121 XYZ Point = itDomainMeshes->GetNode(*(itPolyIndices++));
122 Points3D.push_back(Point);
126 ArrayPoints2D.push_back(Points2D);
129 XY HolePoint = (Points2D[0] + Points2D[(int)Points2D.size()/2])/2.0;
130 HolePoints.push_back(HolePoint);
139 for (j = 0; j < i; ++j)
142 if (
GetLength((ConvertRef.
Normal + PlaneParams[j].Normal)) < 0.000000001)
144 double dDist =
DotProduct(PlaneParams[j].Normal, (PlaneParams[j].RefPoint - ConvertRef.
RefPoint));
152 vector<XY> SeededSides;
154 Triangulate(ArrayPoints2D, HolePoints, TriangleMesh, ConvertRef);
161 vector<XY> SeededSides;
163 Triangulate(ArrayPoints2D, HolePoints, TriangleMesh, ConvertRef);
174 list<int>::const_iterator itIndices;
175 itIndices = Indices.begin();
178 vector<XYZ> Points3D;
179 for (
int iNode = 0; iNode < numNodes; ++iNode)
181 XYZ Point = DomainMesh.
GetNode(*(itIndices++));
182 Points3D.push_back(Point);
184 if (Points3D.size() < 3)
201 vector<XYZ>::iterator itPoints3D;
203 for (itPoints3D = Points3D.begin(); itPoints3D != Points3D.end(); ++itPoints3D)
209 Points2D.push_back(Point2D);
216 for (
int i = 0; i < iNumNodes; ++i)
219 Point += Normal * dDist;
226 vector<XY> SeededSides;
227 vector<XY>::iterator itPoints;
230 for (itPoints = Points.begin(); itPoints != Points.end(); )
233 if (itPoints == Points.begin())
235 StartPoint = *itPoints;
237 XY P1 = *(itPoints++);
239 if (itPoints != Points.end())
247 SeededSides.push_back(P1);
249 int iNumSplits = int(floor(dLength /
m_Seed));
250 for (
int i = 1; i < iNumSplits; ++i)
252 double u = double(i) / double(iNumSplits);
253 XY Point = P1 + (P2 - P1)*u;
254 SeededSides.push_back(Point);
258 Points.insert(Points.begin(), SeededSides.begin(), SeededSides.end());
264 stringstream Switches;
267 double dMinAngle = 20;
292 Switches <<
"pzAPBq" << setiosflags(ios::fixed) << setprecision(20) << dMinAngle <<
"a" << dMaxArea;
295 triangleio TriangleInput;
296 triangleio TriangleOutput;
299 ctx = triangle_context_create();
301 triangle_context_options(ctx, (
char*)Switches.str().c_str());
303 memset(&TriangleInput, 0,
sizeof(TriangleInput));
304 memset(&TriangleOutput, 0,
sizeof(TriangleOutput));
307 vector<vector<XY> >::iterator itArrayPolygonPoints;
309 for (itArrayPolygonPoints = PolygonPoints.begin(); itArrayPolygonPoints != PolygonPoints.end(); ++itArrayPolygonPoints)
311 iNumPoints += (int)(*itArrayPolygonPoints).size();
313 TriangleInput.pointlist =
new REAL[iNumPoints * 2];
314 TriangleInput.numberofpoints = iNumPoints;
316 TriangleInput.segmentlist =
new int[iNumPoints * 2];
317 TriangleInput.numberofsegments = iNumPoints;
320 for (itArrayPolygonPoints = PolygonPoints.begin(); itArrayPolygonPoints != PolygonPoints.end(); ++itArrayPolygonPoints)
322 vector<XY>::iterator itPolyPoints;
324 int iNumPolyPoints = (int)(*itArrayPolygonPoints).size();
326 for (itPolyPoints = (*itArrayPolygonPoints).begin(); itPolyPoints != (*itArrayPolygonPoints).end(); ++itPolyPoints)
329 TriangleInput.pointlist[i * 2] = itPolyPoints->x;
330 TriangleInput.pointlist[i * 2 + 1] = itPolyPoints->y;
333 TriangleInput.segmentlist[i * 2] = i;
334 if (j < iNumPolyPoints - 1)
336 TriangleInput.segmentlist[i * 2 + 1] = i + 1;
340 TriangleInput.segmentlist[i * 2 + 1] = StartIndex;
351 vector<XY>::iterator itHolePoints;
352 int iNumHoles = (int)HolePoints.size();
353 TriangleInput.holelist =
new REAL[iNumHoles * 2];
354 TriangleInput.numberofholes = iNumHoles;
355 for (itHolePoints = HolePoints.begin(), i = 0; itHolePoints != HolePoints.end(); ++itHolePoints, ++i)
357 TriangleInput.holelist[i * 2] = itHolePoints->x;
358 TriangleInput.holelist[i * 2 + 1] = itHolePoints->y;
362 triangle_mesh_create(ctx, &TriangleInput);
365 delete[] TriangleInput.pointlist;
366 delete[] TriangleInput.segmentlist;
368 delete[] TriangleInput.holelist;
370 triangle_mesh_copy(ctx, &TriangleOutput, 1, 1);
373 for (
int i = 0; i<TriangleOutput.numberofpoints; ++i)
376 Point.
x = TriangleOutput.pointlist[i * 2];
377 Point.
y = TriangleOutput.pointlist[i * 2 + 1];
378 Points2D.push_back(Point);
380 vector<XYZ> Points3D;
381 vector<XYZ>::iterator itPoints3D;
384 for (itPoints3D = Points3D.begin(); itPoints3D != Points3D.end(); ++itPoints3D)
386 OutputMesh.
AddNode(*itPoints3D);
389 for (
int i = 0; i<TriangleOutput.numberoftriangles; ++i)
396 triangle_free(TriangleOutput.pointlist);
397 triangle_free(TriangleOutput.trianglelist);
399 triangle_context_destroy(ctx);
406 vector<XY>::iterator itPoints2D;
408 for (itPoints2D = Points2D.begin(); itPoints2D != Points2D.end(); ++itPoints2D)
411 Point3D = ConvertRef.
XAxis * itPoints2D->
x;
412 Point3D += ConvertRef.
YAxis * itPoints2D->
y;
416 Points3D.push_back(Point3D);
#define TGERROR(MESSAGE)
Macros used to report the file name and line number to the TexGenError and TexGenLog functions.
void Convert2DTo3DCoordinates(vector< XY > &Points2D, vector< XYZ > &Points3D, PLANEPARAMS &ConvertRef)
Convert local 2D coordinates to global 3D coordinates.
bool Triangulate(vector< vector< XY > > &PolygonPoints, vector< XY > &HolePoints, CMesh &OutputMesh, PLANEPARAMS &ConvertRef)
Triangulate the domain faces.
void Convert3DTo2DCoordinates(vector< XYZ > &Points3D, PLANEPARAMS &ConvertRef, vector< XY > &Points2D)
Convert global 3D coordinates to local 2D coordinates.
double m_Seed
Seed used for calculating boundary edge points.
virtual ~CMeshDomainPlane(void)
void MeshDomainPlanes(bool bPeriodic)
void OffsetMeshPoints(CMesh &Mesh, XYZ &Normal, double dDist)
Offsets points in mesh by given distance in direction of normal.
vector< vector< int > > m_PolygonNumVertices
Number of polygon vertices on each face. Number of outer vector members = number of faces.
bool ConvertDomainPointsTo2D(const list< int > &Indices, CMesh &DomainMesh, int numNodes, vector< XY > &Points2D, PLANEPARAMS &ConvertRef)
Convert points on one domain surface to local 2D points.
void SeedSides(vector< XY > &Points)
Calculates seed points along domain edge.
bool m_bFillEnds
True if yarn areas are to be removed from domain.
vector< CMesh > m_DomainMeshes
Vector of meshes used to store domain plane meshes.
vector< CMesh > m_TriangulatedMeshes
Vector of triangulated domain plane meshes.
Defines the nodes and elements of a surface or volume mesh.
int GetNumNodes() const
Return the number of nodes.
const int AddNode(XYZ Node)
Append a node to the list of nodes, the integer returns the index of the node
const XYZ & GetNode(int iIndex) const
Get the node with given ID.
static int GetNumNodes(ELEMENT_TYPE Type)
Get the number of nodes a particular element type contains.
const list< int > & GetIndices(ELEMENT_TYPE ElemType) const
Get the element indices of a given element type.
void SetNode(int iIndex, XYZ Node)
Set the node at given index.
Namespace containing a series of customised math operations not found in the standard c++ library.
double GetLength(const XYZ &Point1, const XYZ &Point2)
Get the length between two points.
WXYZ & Normalise(WXYZ &Quaternion)
Normalise the quaternion and return it.
double DotProduct(const XYZ &left, const XYZ &right)
Get the dot product of two vectors.
XYZ CrossProduct(const XYZ &left, const XYZ &right)
Get the cross product of two vectors.
Structure which contains information for transformation from 3D to 2D plane.
Struct for representing points in 2D space.
Struct for representing points in 3D space.