Introduction

Getting Started

Programming Guides

API Reference

Additional Resources

ModelBuilder.h
1 #pragma once
2 #include <functional>
3 #include <unordered_map>
4 #include <unordered_set>
5 
6 #include "HIOUtilityHTML.h"
7 #include "GrownKeyPath.h"
8 #include "Line.h"
9 #include "Text.h"
10 #include "lazy.h"
11 #include "sc_store.h"
12 #include "sc_store_utils.h"
13 #include "sc_store_utils_text.h"
14 #include "sc_assemblytree.h"
15 
16 namespace hio_html_internal
17 {
19  {
20  public:
21  static void Init(HOutputHandler * handler);
22  static void Reset();
23 
24  static void ReportProgress(std::string const & message);
25  static void ReportProgress(float percent);
26 
27  private:
28  static HOutputHandler * outputHandler;
29  };
30 
31  static inline unsigned char unit_to_byte(float const & a)
32  {
33  uint32_t v = extract_uint32_t(a);
34 
35  v &= 0x7FFFFFFF;
36  if (v < 0x3B800000)
37  return 0;
38 
39  v--;
40 
41  uint32_t exp = v >> 23;
42  uint32_t man = (v & 0x007FFFFF) | 0x00800000;
43 
44  return (unsigned char)(man >> (16 + 126 - exp));
45  }
46 
47  struct RGBA32Color
48  {
49  RGBA32Color()
50  {}
51 
52  RGBA32Color(RGBAColor const & c)
53  {
54  r = unit_to_byte(c[0]);
55  g = unit_to_byte(c[1]);
56  b = unit_to_byte(c[2]);
57  a = unit_to_byte(c[3]);
58  }
59 
60  RGBA32Color(RGBColor const & c)
61  {
62  r = unit_to_byte(c[0]);
63  g = unit_to_byte(c[1]);
64  b = unit_to_byte(c[2]);
65  a = 255;
66  }
67 
68  unsigned char r;
69  unsigned char g;
70  unsigned char b;
71  unsigned char a;
72  };
73 
74  enum class MeshType
75  {
76  Face,
77  Line,
78  Point,
79  Misc,
80  };
81 
82  enum class MaterialType : char
83  {
84  NoColor = 0,
85  ByIndex,
86  RGBColor,
87  RGBAColor,
88  };
89 
91  {
92  MeshEnvironment(MeshEnvironment const & other) = delete;
93  void operator= (MeshEnvironment const & other) = delete;
94 
95  public:
96  MeshEnvironment() {}
98 
99  void operator= (MeshEnvironment && other);
100 
101  void PatchMeshPointers();
102  bool IsValid() const;
103 
104  SC::Store::Mesh mesh;
105  bool isText;
106  std::vector<SC::Store::Point> points;
107  std::vector<SC::Store::Normal> normals;
108  std::vector<SC::Store::UV> uvs;
109  std::vector<SC::Store::RGBA32> rgba32s;
110  };
111 
113  {
114  public:
115  MeshStatistics();
116 
117  size_t triangleCount;
118  size_t polylineCount;
119  size_t freePointCount;
120  };
121 
122  class HTMLLogger : public SC::Store::Logger {
123  public:
124  virtual void Message(const char * message) const
125  {
126  printf("%s\n", message);
127  }
128  };
129 
131  {
132  public:
133  ModelStatistics();
134 
135  size_t instanceCount;
136 
137  size_t uniqueFaceMeshCount;
138  size_t uniqueLineMeshCount;
139  size_t uniquePointMeshCount;
140  size_t uniqueMiscMeshCount;
141  size_t uniqueImageCount;
142 
143  size_t faceMeshCount;
144  size_t lineMeshCount;
145  size_t pointMeshCount;
146  size_t miscMeshCount;
147  };
148 
149  enum class Visibility : uint32_t {
150  None = 0,
151  Face = 1 << 0,
152  Line = 1 << 1,
153  Point = 1 << 2,
154  Text = 1 << 3,
155  };
156 
158  {
159  public:
160  enum class Batching {
161  Disabled,
162  SegmentLines,
163  SegmentGeometry,
164  };
165 
167 
168  public:
169  std::function<bool(HC_KEY)> assemblyTreeGenerateChild;
170  ModelStatistics * statistics;
171  SC::Store::Color defaultColor;
172  Batching batching;
173  bool excludeInvisibleGeometry;
174  bool excludeLines;
175  bool excludeVertexNormals;
176  bool excludeVertexUVs;
177  bool excludeVertexColors;
178  bool generateImplicitEdges;
179  bool forceNormals;
180  bool verbose;
181  bool storeGeometryUserData;
182  bool forceCounterClockwiseWinding;
183  bool groupAllMeshInstances;
184  bool generateAssemblyTree;
185  bool markMeshesAsManifold;
186  };
187 
188  enum class PolygonHandedness : size_t
189  {
190  Left,
191  Right,
192  None,
193 
194  Default = Left
195  };
196 
198  {
199  public:
200  SimpleMaterial() {}
201  SimpleMaterial(SC::Store::Color const & color);
202  SimpleMaterial(SC::Store::Color const & color, float mirror, float specular, float gloss, std::string texture);
203 
204  size_t Hash() const;
205  bool operator== (SimpleMaterial const & other) const;
206 
207  public:
208  SC::Store::Color color;
209  float mirror;
210  float specular;
211  float gloss;
212  std::string texture;
213  };
214 
216  {
217  size_t operator() (SimpleMaterial const & material) const { return material.Hash(); }
218  };
219 
221  {
222  public:
223  AggregateMaterial() {}
224 
226  ModelBuilderConfig const & config,
227  KeyArray const & key_path,
228  std::string const & materialMappingKit,
229  Lazy<StringArray> & materialMap);
230 
232  KeyArray const & key_path,
233  AggregateMaterial const & segmentMaterial,
234  std::string const & localMaterialMappingKit,
235  Lazy<StringArray> & materialMap);
236 
237  private:
238  void Initialize(
239  KeyArray const & key_path,
240  std::string const & materialMappingKit,
241  Lazy<StringArray> & materialMap);
242 
243  public:
244  SimpleMaterial face;
245  SimpleMaterial line;
246  SimpleMaterial point;
247  SimpleMaterial text;
248  };
249 
251  {
252  public:
253  NetClipRegions() : loops_array(), spaces(), operations(), clip_region_key() {}
254 
255  public:
256  using ClipRegionArray = std::vector<PointArray>;
257 
258  ClipRegionArray loops_array;
259  StringArray spaces;
260  StringArray operations;
261  mutable SC::Store::ClipRegionKey clip_region_key;
262  };
263 
264  struct MatrixEq
265  {
266  bool operator() (SC::Store::Matrix3d const & matrix1, SC::Store::Matrix3d const & matrix2) const;
267  };
268 
269  struct MatrixHasher
270  {
271  size_t operator() (SC::Store::Matrix3d const & matrix) const;
272  };
273 
275  {
276  size_t operator() (SC::Store::MaterialKeys const & materialMap) const;
277  };
279  {
280  size_t KeyHasher(const HC_KEY & key) const
281  {
282  size_t hash = (size_t)key;
283  hash = (hash ^ 61) ^ (hash >> 16);
284  hash = hash + (hash << 3);
285  hash = hash ^ (hash >> 4);
286  hash = hash * 0x27d4eb2d;
287  hash = hash ^ (hash >> 15);
288  return hash;
289  }
290 
291  size_t operator() (std::tuple<HC_KEY, PolygonHandedness, Visibility> const & tuple) const {
292  return KeyHasher(std::get<0>(tuple)) + static_cast<size_t>(std::get<1>(tuple)) + static_cast<uint32_t>(std::get<2>(tuple));
293  }
294  };
295 
296  struct RgbHasher
297  {
298  size_t operator() (RGBColor const & color) const;
299  };
300 
302  Visibility geometryVisibility;
303  Visibility lightingVisibility;
304  };
305 
307  {
308  typedef std::vector<RGBA32Color> RGBA32ColorArray;
309 
310  public:
311  ~ModelBuilder();
312 
313  ModelBuilder(
314  ModelBuilderConfig const & config,
315  SC::Store::Logger & logger,
316  SC::Store::Model & model,
317  SC::Store::InclusionKey inclusionKey,
318  HOutputHandler * handler,
319  HOutputHandlerOptions const * options);
320 
321  bool PreUpdateBySegPath(GrownKeyPath const & segPath);
322  void UpdateBySegPath(GrownKeyPath const & segPath);
323 
324  private:
325  static Lazy<PolygonHandedness> LazyObtainPolygonHandedness(KeyArray const & segPath);
326  Lazy<AggregateMaterial> LazyObtainAggregateMaterial(KeyArray const & segPath, Lazy<StringArray> & materialMap);
327  Lazy<NetClipRegions> LazyObtainNetClipRegions(KeyArray const & segPath);
328 
329  void UpdateBySegPath(GrownKeyPath const & segPath, HC_KEY segKey, bool followReferences);
330 
331  void FollowReference(
332  GrownKeyPath const & segPath,
333  HC_KEY referenceKey,
334  Lazy<TextAttributes> & textAttributes,
335  Lazy<StringArray> & materialMap,
336  Lazy<AggregateMaterial> & material,
337  Lazy<GeomAndLightVisibility> & visibility,
338  Lazy<PolygonHandedness> & polygonHandedness,
339  Lazy<NetClipRegions> & netClipRegions);
340 
341  void TryInsertBatchedMeshes(
342  KeyArray const & keyPath,
343  HC_KEY segmentKey,
344  Lazy<SC::Store::MatrixKey> & matrixKey,
345  Lazy<AggregateMaterial> & material,
346  Visibility visibility,
347  Visibility lightingVisibility,
348  PolygonHandedness polygonHandedness,
349  NetClipRegions const & net_clip_regions);
350 
351  void TryInsert(
352  KeyArray const & keyPath,
353  HC_KEY geometryKey,
354  Lazy<StringArray> & materialMap,
355  Lazy<Matrix> & matrix,
356  Lazy<SC::Store::MatrixKey> & matrixKey,
357  Lazy<AggregateMaterial> & segmentMaterial,
358  Lazy<TextAttributes> & textAttributes,
359  std::string const * localMaterialMappingKit,
360  Visibility visibility,
361  Visibility lightingVisibility,
362  PolygonHandedness polygonHandedness,
363  NetClipRegions const & net_clip_regions);
364 
365  void AddToMeshBatch(
366  HC_KEY geometryKey,
367  Lazy<StringArray> & materialMap,
368  Lazy<Matrix> & matrix,
369  Lazy<TextAttributes> & textAttributes,
370  KeyArray const & keyPath,
371  Visibility visibility,
372  PolygonHandedness polygonHandedness);
373 
374  SC::Store::MeshKey ObtainMeshKey(
375  Visibility visibility,
376  HC_KEY geometryKey,
377  Lazy<StringArray> & materialMap,
378  Lazy<Matrix> & matrix,
379  Lazy<TextAttributes> & textAttributes,
380  KeyArray const & keyPath,
381  PolygonHandedness polygonHandedness,
382  Visibility & usedVisibilities,
383  bool & outIsText);
384 
385  SC::Store::ImageKey ObtainImageKey(HC_KEY imageKey);
386  SC::Store::MatrixKey ObtainMatrixKey(Matrix const & matrixKit);
387  SC::Store::MaterialKey ObtainMaterialKey(SimpleMaterial const & material, KeyArray const & keyPath);
388  SC::Store::MaterialMapKey ObtainMaterialMapKey(SC::Store::MaterialKeys && materialMap);
389 
390  SC::Store::Mesh * CreateShellWireframeMesh(
391  Visibility visibility,
392  Lazy<StringArray> & materialMap,
393  HC_KEY shellKey,
394  PointArray const & points,
395  std::vector<Line> const & lines,
396  PolygonHandedness polygonHandedness);
397 
398  SC::Store::Mesh * CreateLineMesh(
399  Visibility visibility,
400  HC_KEY lineKey,
401  PolygonHandedness polygonHandedness);
402 
403  SC::Store::Mesh * CreateShellMesh(
404  Visibility visibility,
405  HC_KEY shellKey,
406  Lazy<StringArray> & materialMap,
407  KeyArray const & keyPath,
408  PolygonHandedness polygonHandedness);
409 
410  SC::Store::Mesh * CreateTextMesh(
411  HC_KEY textKey,
412  Lazy<Matrix> & matrix,
413  TextAttributes const & textAttributes,
414  PolygonHandedness polygonHandedness);
415 
416  SC::Store::Mesh * CreateMesh(
417  Visibility visibility,
418  HC_KEY key,
419  Lazy<StringArray> & materialMap,
420  Lazy<Matrix> & matrix,
421  Lazy<TextAttributes> & textAttributes,
422  KeyArray const & keyPath,
423  PolygonHandedness polygonHandedness,
424  bool & outIsText);
425 
426  SC::Store::Mesh * CreateMassagedMesh(
427  Visibility visibility,
428  HC_KEY geometryKey,
429  Lazy<StringArray> & materialMap,
430  Lazy<Matrix> & matrix,
431  Lazy<TextAttributes> & textAttributes,
432  KeyArray const & keyPath,
433  PolygonHandedness polygonHandedness);
434 
435  VectorArray GetShellVertexNormals(HC_KEY shellKey);
436 
437  FloatArray GetShellVertexUVs(HC_KEY shellKey);
438 
439  RGBA32ColorArray GetVertexColors(
440  HC_KEY shellKey,
441  Lazy<StringArray> & materialMap,
442  std::string component);
443 
444  bool ApplyTexture(
445  SC::Store::Material & faceMaterial,
446  std::string const & textureDefinition,
447  KeyArray const & keyPath);
448 
449  SC::Store::MaterialKey InsertMaterial(SimpleMaterial const & material, KeyArray const & keyPath);
450  SC::Store::MatrixKey InsertMatrix(SC::Store::Matrix3d const & matrix);
451  SC::Store::MeshKey InsertMesh(SC::Store::Mesh const & mesh);
452  SC::Store::MaterialMapKey InsertMaterialMap(SC::Store::MaterialKeys const & materialMap);
453 
454  SC::Store::InstanceKey Instance(
455  SC::Store::MeshKey meshKey,
456  SC::Store::MatrixKey matrixKey,
457  AggregateMaterial const & material,
458  Visibility visibility,
459  Visibility lightingVisibility,
460  bool isText,
461  NetClipRegions const & net_clip_regions,
462  KeyArray const & keyPath);
463 
464  void CanonicalizeElementIndexMapping(
465  SC::Store::MaterialKeys & materialMap,
466  std::vector<size_t> & faceToElementIndex);
467 
468  std::vector<size_t> ObtainFaceToElementIndexMapping(
469  KeyArray const & keyPath,
470  HC_KEY shellKey,
471  SC::Store::Mesh & mesh);
472 
473  MeshEnvironment & ObtainCompatibleBatchedMesh(SC::Store::Mesh const & overlayMesh, bool isText);
474  void MergeIntoBatchedMesh(SC::Store::Mesh & overlayMesh, bool isText);
475  void MergeSegmentLines(HC_KEY & segKey);
476 
477  SC::Store::Mesh & MeshFromCache(PolygonHandedness polygonHandedness);
478  void MeshElementsFromCache(std::vector<SC::Store::MeshElement> & meshElements, size_t count);
479  void ReleaseToCache(SC::Store::Mesh & mesh);
480  void ReleaseToCache(std::vector<SC::Store::MeshElement> & meshElements);
481  void FinalizeTableDataKey();
482 
483  private:
484  ModelBuilder(ModelBuilder const &) = delete;
485  void operator= (ModelBuilder const &) = delete;
486 
487  private:
488  ModelBuilderConfig const config;
489  SC::Store::Logger & logger;
490  SC::Store::Model & model;
491  SC::Store::InclusionKey inclusionKey;
492 
493  HC_KEY scratchSeg;
494 
495  std::unique_ptr<SC::Store::AssemblyTree> assemblyTree;
496  std::vector<Lazy<uint32_t>> assemblyTreePath;
497  std::unordered_map<HC_KEY, unsigned> generatedSegmentNameIds;
498 
499  std::vector<MeshEnvironment> batchedMeshEnvironments;
500  std::unordered_set<HC_KEY> batchedLines;
501  std::vector<HC_KEY> createdShells;
502 
503  SC::Store::Mesh cachedMesh;
504  std::vector<SC::Store::MeshElement> cachedMeshElements;
505  std::vector<size_t> cachedIndices1;
506  std::vector<size_t> cachedIndices2;
507 
508  PointArray pointBuffer;
509  VectorArray vertexNormalBuffer;
510  FloatArray vertexUVBuffer;
511  RGBA32ColorArray vertexColorBuffer;
512 
513  std::unordered_map<SimpleMaterial, SC::Store::MaterialKey, SimpleMaterialHasher> seenMaterials;
514  std::unordered_map<SC::Store::Matrix3d, SC::Store::MatrixKey, MatrixHasher, MatrixEq> seenMatrices;
515  std::unordered_map<HC_KEY, SC::Store::ImageKey> seenImages;
516  std::unordered_map<SC::Store::MaterialKeys, SC::Store::MaterialMapKey, MaterialKeysHasher> seenMaterialMaps;
517 
518  std::unordered_map <
519  std::tuple<HC_KEY, PolygonHandedness, Visibility>,
520  std::pair<SC::Store::MeshKey, Visibility>,
521  SeenMeshHasher> seenGeometries;
522 
523  std::unordered_map <
524  std::tuple<HC_KEY, PolygonHandedness, Visibility>,
525  std::vector<std::pair<SC::Store::MeshKey, Visibility>>,
526  SeenMeshHasher> seenBatchedGeometries;
527 
528  std::vector<std::pair<MeshStatistics, MeshType>> meshesStats; // Indexed by MeshKey.
529 
530  SC::Store::DataKey tableDataKey;
531  std::vector<std::pair<SC::Store::MeshKey, SC::Store::DataKey>> pendingDataKeys;
532 
533  SC::Store::InstanceKey lastGeneratedInstanceKey;
534 
535  HOutputHandler * outputHandler;
536 
537  std::string driverPath;
538  std::unordered_map<std::string, bool> fontOutlineability;
539  };
540 } //end of hio_html_internal namespace
Definition: lazy.h:6
Definition: ModelBuilder.h:220
Definition: ModelBuilder.h:197
Definition: ModelBuilder.h:306
Definition: ModelBuilder.h:122
Definition: ModelBuilder.h:112
Definition: HDWFMisc.h:22
Definition: ModelBuilder.h:157
Definition: ModelBuilder.h:296
#define HC_KEY
HOutputHandler an abstract base class for model and image output.
Definition: HIOManager.h:1220
Definition: ModelBuilder.h:215
Definition: ModelBuilder.h:278
Definition: ModelBuilder.h:47
Definition: GrownKeyPath.h:7
Definition: HIOManager.h:865
Definition: ModelBuilder.h:301
Definition: ModelBuilder.h:264
Definition: ModelBuilder.h:274
Definition: ModelBuilder.h:18
A class used to denote a line segment by its front and back indices.
Definition: Line.h:8
Definition: ModelBuilder.h:130
Definition: ModelBuilder.h:250
Definition: ModelBuilder.h:90
Definition: ModelBuilder.h:269