CATCH
 All Classes Functions
CatTrimeshtJtk.h
1 
2 #pragma once
3 #include "CatTrimesh.h"
4 
5 #include <JtTk/JtkEntity.h>
6 #include <JtTk/JtkCADImporter.h>
7 #include <JtTk/JtkHierarchy.h>
8 #include <JtTk/JtkEntityFactory.h>
9 #include <JtTk/JtkTriStripSet.h>
11 //
12 // Class : CatTrimesh
13 //
14 // Description : An implementation of CatTrimesh specific to triangle meshes output from CatJTK
15 //
16 //}%
18 {
19 
20 public:
21 
22 
26  int getTotalVerticeCount(JtkEntityPtr<JtkTriStripSet> &tStripSet)
27  {
28  float *vertex= NULL,
29  *normal= NULL,
30  *color= NULL,
31  *texture= NULL;
32  int vertexCount= -1,
33 
34  normCount= -1,
35  colorCount= -1,
36  textCount= -1;
37 
38  int totalCount = 0;
39  for(int set = 0; set < tStripSet->numOfSets(); set++)
40  {
41  tStripSet->getInternal(vertex, vertexCount, normal, normCount,
42  color, colorCount, texture, textCount, set);
43  //create double array and copy floats into new array
44 
45  //_iVertices = vertex;
46  totalCount += vertexCount;
47  }
48  return totalCount;
49  }
50 
51  /***
52  * public constructor
53  * @param tStripSet`A JtkEntityPtr of type JtkTriStripSet from OpenJTK holding all the triangles that make up this mesh
54  * @param jtkId The concatonnated ID for this model mesh, the JTK id holds all the ids in the hierarchy
55  * @param xform the local transform of this triangle mesh
56  */
57  CatTrimeshJtk(JtkEntityPtr<JtkTriStripSet> &tStripSet, std::string jtkId, JtkTransform *& xform)
58  {
59 
60  float *vertex= NULL,
61  *normal= NULL,
62  *color= NULL,
63  *texture= NULL;
64  int vertexCount= -1,
65 
66  normCount= -1,
67  colorCount= -1,
68  textCount= -1;
69 
70  int numVert = getTotalVerticeCount(tStripSet);
71  int numSets = tStripSet->numOfSets();
72 
73  //foreach set, the number of triangles is equal to the number of vertices minus 2
74  //therefore the number of total triangles is equal to the total number of vertices minus 2*(number of sets)
75 
76  int numTriangles = numVert - 2 * numSets;
77  int numIndices = 3*numTriangles;
78  //each set of vertices
79  _iVertices = new double[numVert*3]; //number of vertices counted
80  _iTriangles = new unsigned int[numIndices]; //size = 3 * num of triangles
81  _iNbVertices = numVert;
82  _iNbTriangles = numTriangles;
83 
84  int tv = 0; //total vertex array index
85  int tri = 0; //triangle index
86 
87  for(int set = 0; set < tStripSet->numOfSets(); set++)
88  {
89  tStripSet->getInternal(vertex, vertexCount, normal, normCount,
90  color, colorCount, texture, textCount, set);
91  //create double array and copy floats into new array
92 
93  //copy vertices into total vertice array
94  for(int lv = 0; lv < vertexCount*3; lv+=3)
95  {
96  //update indices
97  if(tri < numIndices)
98  {
99  //except the first two of a set, every vertice that is added represents another triangle
100  if(lv/3 > 1)
101  {
102  _iTriangles[tri] = (tv/3)-2;
103  _iTriangles[tri+1] = (tv/3)-1;
104  _iTriangles[tri+2] = (tv/3);
105 
106  tri += 3;
107  }
108  }
109  //add vertex
110  if(tv<numVert*3)
111  {
112  _iVertices[tv] = vertex[lv];
113  _iVertices[tv+1] = vertex[lv+1];
114  _iVertices[tv+2] = vertex[lv+2];
115  tv +=3;
116  }
117  }
118  }
119 
120  //assume scale factor is 1
121  _iScale = 1.0;
122 
123  //set jtkId to null, must be checked or set at a future time
124  //heap allocate a new string
125  int len =jtkId.length();
126  _jtkId = new char[len+1];
127 
128  //copy the string contents to heap allocated string
129  jtkId.copy(_jtkId, jtkId.length(), 0);
130  //null terminate c string
131  _jtkId[len] = '\0';
132  //set Transform
133  _iLocalTransform = new double[16];
134  xform->getTElements(_iLocalTransform);
135 
136  //not sure where geometric offset is used, set to 0
137  _iOffset = 1.0;
138 
139 
140  }
141 
147  const char* jtkId()
148  {
149  return _jtkId;
150  }
154  const unsigned int iNbVertices()
155  {
156  return _iNbVertices;
157  }
161  const double *iVertices()
162  {
163  return _iVertices;
164  }
168  const unsigned int iNbTriangles()
169  {
170  return _iNbTriangles;
171  }
175  const unsigned int *iTriangles()
176  {
177  return _iTriangles;
178  }
179 
183  const double iScale()
184  {
185  return _iScale;
186  }
190  const double iOffset()
191  {
192  return _iOffset;
193  }
197  const double *iLocalTransform()
198  {
199  return _iLocalTransform;
200  }
201 
202 private:
203  char* _jtkId;
204  unsigned int _iNbVertices;
205  double * _iVertices;
206  unsigned int _iNbTriangles;
207  unsigned int *_iTriangles;
208  double _iScale;
209  double _iOffset;
210  double *_iLocalTransform;
211 
212 
213 protected:
214  ~CatTrimeshJtk()
215  {
216  //free arrays
217  delete [] _iTriangles;
218  delete [] _iLocalTransform;
219  delete [] _iVertices;
220  delete [] _jtkId;
221 
222 
223  }
224 };
const unsigned int iNbVertices()
Definition: CatTrimeshtJtk.h:154
const char * jtkId()
Definition: CatTrimeshtJtk.h:147
const unsigned int * iTriangles()
Definition: CatTrimeshtJtk.h:175
int getTotalVerticeCount(JtkEntityPtr< JtkTriStripSet > &tStripSet)
Definition: CatTrimeshtJtk.h:26
const double * iLocalTransform()
Definition: CatTrimeshtJtk.h:197
const unsigned int iNbTriangles()
Definition: CatTrimeshtJtk.h:168
const double * iVertices()
Definition: CatTrimeshtJtk.h:161
const double iOffset()
Definition: CatTrimeshtJtk.h:190
const double iScale()
Definition: CatTrimeshtJtk.h:183
Definition: CatTrimesh.h:13
Definition: CatTrimeshtJtk.h:17