GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 0 / 1 / 1
Functions: -% 0 / 1 / 1
Branches: -% 0 / 1 / 1

src/utilities/geometry/ThreeJS.hpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #ifndef UTILITIES_GEOMETRY_THREEJS_HPP
7 #define UTILITIES_GEOMETRY_THREEJS_HPP
8
9 #include "../UtilitiesAPI.hpp"
10
11 #include "Point3d.hpp"
12 #include "Transformation.hpp"
13
14 #include "../core/Logger.hpp"
15
16 #include <vector>
17 #include <map>
18 #include <boost/optional.hpp>
19
20 namespace Json {
21 class Value;
22 }
23
24 namespace openstudio {
25
26 class ThreeScene;
27 class ThreeMaterial;
28
29 /// enum for materials
30 enum ThreeSide
31 {
32 FrontSide = 0,
33 BackSide = 1,
34 DoubleSide = 2
35 };
36
37 /// identifies ThreeJS faces in OpenStudio format (e.g. unlimited number of vertices)
38 UTILITIES_API unsigned openstudioFaceFormatId();
39
40 /// convert RGB to unsigned
41 UTILITIES_API unsigned toThreeColor(unsigned r, unsigned g, unsigned b);
42
43 /// convert string to unsigned
44 UTILITIES_API unsigned toThreeColor(const std::string& s);
45
46 /// convert object name to material name
47 UTILITIES_API std::string getObjectThreeMaterialName(const std::string& iddObjectType, const std::string& name);
48
49 /// convert surface type to material name
50 UTILITIES_API std::string getSurfaceTypeThreeMaterialName(const std::string& surfaceType);
51
52 /// Create a ThreeMaterial
53 UTILITIES_API ThreeMaterial makeThreeMaterial(const std::string& name, unsigned color, double opacity, unsigned side, unsigned shininess = 50,
54 const std::string& type = "MeshPhongMaterial");
55
56 /// Add a ThreeMaterial to a list of materials and map of material name to material
57 UTILITIES_API void addThreeMaterial(std::vector<ThreeMaterial>& materials, std::map<std::string, std::string>& materialMap,
58 const ThreeMaterial& material);
59
60 /// Get a material id out of material map
61 UTILITIES_API std::string getThreeMaterialId(const std::string& materialName, std::map<std::string, std::string>& materialMap);
62
63 /// Create the standard ThreeMaterials
64 UTILITIES_API std::vector<ThreeMaterial> makeStandardThreeMaterials();
65
66 /// format a UUID, to limit dependencies UUIDs must be generated outside of this code
67 UTILITIES_API std::string toThreeUUID(const std::string& uuid);
68
69 UTILITIES_API std::string fromThreeUUID(const std::string& uuid);
70
71 /// format a list of vertices
72 UTILITIES_API std::vector<double> toThreeVector(const Point3dVector& vertices);
73
74 UTILITIES_API Point3dVector fromThreeVector(const std::vector<double>& vertices);
75
76 /// format a Transformation matrix
77 UTILITIES_API std::vector<double> toThreeMatrix(const Transformation& matrix);
78
79 UTILITIES_API Transformation toThreeMatrix(const std::vector<double>& matrix);
80
81 /// ThreeGeometryData holds the geometry data for an object
82 class UTILITIES_API ThreeGeometryData
83 {
84 public:
85 ThreeGeometryData(const std::vector<double>& vertices, const std::vector<size_t>& faces);
86 std::vector<double> vertices() const;
87 std::vector<size_t> normals() const;
88 std::vector<size_t> uvs() const;
89 std::vector<size_t> faces() const;
90 double scale() const;
91 bool visible() const;
92 bool castShadow() const;
93 bool receiveShadow() const;
94 bool doubleSided() const;
95
96 private:
97 friend class ThreeGeometry;
98 ThreeGeometryData(const Json::Value& value);
99 Json::Value toJsonValue() const;
100
101 std::vector<double> m_vertices;
102 std::vector<size_t> m_normals;
103 std::vector<size_t> m_uvs;
104 std::vector<size_t> m_faces;
105 double m_scale;
106 bool m_visible;
107 bool m_castShadow;
108 bool m_receiveShadow;
109 bool m_doubleSided;
110 };
111
112 /// ThreeGeometry holds the geometry for an object
113 class UTILITIES_API ThreeGeometry
114 {
115 public:
116 ThreeGeometry(const std::string& uuid, const ::std::string& type, const ThreeGeometryData& data);
117 std::string uuid() const;
118 std::string type() const;
119 ThreeGeometryData data() const;
120
121 private:
122 friend class ThreeScene;
123 ThreeGeometry(const Json::Value& value);
124 Json::Value toJsonValue() const;
125
126 std::string m_uuid;
127 std::string m_type;
128 ThreeGeometryData m_data;
129 };
130
131 /// ThreeMaterial defines a rendering material
132 class UTILITIES_API ThreeMaterial
133 {
134 public:
135 ThreeMaterial(const std::string& uuid, const std::string& name, const ::std::string& type, unsigned color, unsigned ambient, unsigned emissive,
136 unsigned specular, unsigned shininess, double opacity, bool transparent, bool wireframe, unsigned side);
137 std::string uuid() const;
138 std::string name() const;
139 std::string type() const;
140 unsigned color() const;
141 unsigned ambient() const;
142 unsigned emissive() const;
143 unsigned specular() const;
144 unsigned shininess() const;
145 double opacity() const;
146 bool transparent() const;
147 bool wireframe() const;
148 unsigned side() const;
149
150 private:
151 friend class ThreeScene;
152 ThreeMaterial(const Json::Value& value);
153 Json::Value toJsonValue() const;
154
155 std::string m_uuid;
156 std::string m_name;
157 std::string m_type;
158 unsigned m_color;
159 unsigned m_ambient;
160 unsigned m_emissive;
161 unsigned m_specular;
162 unsigned m_shininess;
163 double m_opacity;
164 bool m_transparent;
165 bool m_wireframe;
166 unsigned m_side;
167 };
168
169 /// ThreeUserData decorates a ThreeSceneChild with additional information
170 class UTILITIES_API ThreeUserData
171 {
172 public:
173 ThreeUserData();
174 std::string handle() const;
175 std::string name() const;
176
177 /// surfaceType is overloaded as a more general type:
178 /// Surfaces {"Wall", "Floor", "RoofCeiling"}
179 /// SubSurfaces {"FixedWindow", "OperableWindow", "GlassDoor", "Skylight", "TubularDaylightDome", "TubularDaylightDiffuser", "Door", "OverheadDoor"}
180 /// ShadingSurfaces {"SiteShading", "BuildingShading", "SpaceShading"}
181 /// InteriorPartitionSurfaces {"InteriorPartitionSurface"}
182 /// DaylightingControl {"DaylightingControl"}
183 std::string surfaceType() const;
184 std::string surfaceTypeMaterialName() const;
185
186 /// Construction name if any
187 std::string constructionName() const;
188 std::string constructionHandle() const;
189 std::string constructionMaterialName() const;
190
191 /// Parent surface name if any
192 std::string surfaceName() const;
193 std::string surfaceHandle() const;
194
195 /// Parent sub surface name if any
196 std::string subSurfaceName() const;
197 std::string subSurfaceHandle() const;
198
199 /// Parent spaces name if any
200 std::string spaceName() const;
201 std::string spaceHandle() const;
202
203 /// Parent shading name if any
204 std::string shadingName() const;
205 std::string shadingHandle() const;
206
207 /// ThermalZone name if any
208 std::string thermalZoneName() const;
209 std::string thermalZoneHandle() const;
210 std::string thermalZoneMaterialName() const;
211
212 /// SpaceType name if any
213 std::string spaceTypeName() const;
214 std::string spaceTypeHandle() const;
215 std::string spaceTypeMaterialName() const;
216
217 /// BuildingStory name if any
218 std::string buildingStoryName() const;
219 std::string buildingStoryHandle() const;
220 std::string buildingStoryMaterialName() const;
221
222 /// BuildingUnit name if any
223 std::string buildingUnitName() const;
224 std::string buildingUnitHandle() const;
225 std::string buildingUnitMaterialName() const;
226
227 /// ConstructionSet name if any
228 std::string constructionSetName() const;
229 std::string constructionSetHandle() const;
230 std::string constructionSetMaterialName() const;
231
232 std::string outsideBoundaryCondition() const;
233 std::string outsideBoundaryConditionObjectName() const;
234 std::string outsideBoundaryConditionObjectHandle() const;
235 std::string boundaryMaterialName() const;
236 bool coincidentWithOutsideObject() const;
237 std::string sunExposure() const;
238 std::string windExposure() const;
239 double illuminanceSetpoint() const;
240 bool airWall() const;
241
242 std::vector<std::string> airLoopHVACNames() const;
243 std::vector<std::string> airLoopHVACHandles() const;
244 std::vector<std::string> airLoopHVACMaterialNames() const;
245
246 //bool plenum() const;
247 //bool belowFloorPlenum() const;
248 //bool aboveCeilingPlenum() const;
249
250 void setHandle(const std::string& s);
251 void setName(const std::string& s);
252 void setSurfaceType(const std::string& s);
253 void setSurfaceTypeMaterialName(const std::string& s);
254 void setConstructionName(const std::string& s);
255 void setConstructionHandle(const std::string& s);
256 void setConstructionMaterialName(const std::string& s);
257 void setSurfaceName(const std::string& s);
258 void setSurfaceHandle(const std::string& s);
259 void setSubSurfaceName(const std::string& s);
260 void setSubSurfaceHandle(const std::string& s);
261 void setSpaceName(const std::string& s);
262 void setSpaceHandle(const std::string& s);
263 void setShadingName(const std::string& s);
264 void setShadingHandle(const std::string& s);
265 void setThermalZoneName(const std::string& s);
266 void setThermalZoneHandle(const std::string& s);
267 void setThermalZoneMaterialName(const std::string& s);
268 void setSpaceTypeName(const std::string& s);
269 void setSpaceTypeHandle(const std::string& s);
270 void setSpaceTypeMaterialName(const std::string& s);
271 void setBuildingStoryName(const std::string& s);
272 void setBuildingStoryHandle(const std::string& s);
273 void setBuildingStoryMaterialName(const std::string& s);
274 void setBuildingUnitName(const std::string& s);
275 void setBuildingUnitHandle(const std::string& s);
276 void setBuildingUnitMaterialName(const std::string& s);
277 void setConstructionSetName(const std::string& s);
278 void setConstructionSetHandle(const std::string& s);
279 void setConstructionSetMaterialName(const std::string& s);
280 void setOutsideBoundaryCondition(const std::string& s);
281 void setOutsideBoundaryConditionObjectName(const std::string& s);
282 void setOutsideBoundaryConditionObjectHandle(const std::string& s);
283 void setBoundaryMaterialName(const std::string& s);
284 void setCoincidentWithOutsideObject(bool b);
285 void setSunExposure(const std::string& s);
286 void setWindExposure(const std::string& s);
287 void setIlluminanceSetpoint(double d);
288 void setAirWall(bool b);
289 void addAirLoopHVACName(const std::string& s);
290 void addAirLoopHVACHandle(const std::string& s);
291 void addAirLoopHVACMaterialName(const std::string& s);
292 //void setBelowFloorPlenum(bool v);
293 //void setAboveCeilingPlenum(bool v);
294
295 // If set to false, the 4 diags below aren't used
296 bool includeGeometryDiagnostics() const;
297 void setIncludeGeometryDiagnostics(bool includeGeometryDiagnostics);
298 // This Surface is convex
299 bool convex() const;
300 void setConvex(bool b);
301 // The Space it belongs to is convex
302 bool spaceConvex() const;
303 void setSpaceConvex(bool b);
304 // The Space it belongs to is enclosed
305 bool spaceEnclosed() const;
306 void setSpaceEnclosed(bool b);
307 // Given the space it belongs to, it is correctly oriented (or not)
308 bool correctlyOriented() const;
309 void setCorrectlyOriented(bool b);
310
311 private:
312 friend class ThreeSceneChild;
313 ThreeUserData(const Json::Value& value);
314 Json::Value toJsonValue() const;
315
316 std::string m_handle;
317 std::string m_name;
318 std::string m_type;
319 std::string m_surfaceType;
320 std::string m_surfaceTypeMaterialName;
321 std::string m_constructionName;
322 std::string m_constructionHandle;
323 std::string m_constructionMaterialName;
324 std::string m_surfaceName;
325 std::string m_surfaceHandle;
326 std::string m_subSurfaceName;
327 std::string m_subSurfaceHandle;
328 std::string m_spaceName;
329 std::string m_spaceHandle;
330 std::string m_shadingName;
331 std::string m_shadingHandle;
332 std::string m_thermalZoneName;
333 std::string m_thermalZoneHandle;
334 std::string m_thermalZoneMaterialName;
335 std::string m_spaceTypeName;
336 std::string m_spaceTypeHandle;
337 std::string m_spaceTypeMaterialName;
338 std::string m_buildingStoryName;
339 std::string m_buildingStoryHandle;
340 std::string m_buildingStoryMaterialName;
341 std::string m_buildingUnitName;
342 std::string m_buildingUnitHandle;
343 std::string m_buildingUnitMaterialName;
344 std::string m_constructionSetName;
345 std::string m_constructionSetHandle;
346 std::string m_constructionSetMaterialName;
347 std::string m_outsideBoundaryCondition;
348 std::string m_outsideBoundaryConditionObjectName;
349 std::string m_outsideBoundaryConditionObjectHandle;
350 std::string m_boundaryMaterialName;
351 bool m_coincidentWithOutsideObject;
352 std::string m_sunExposure;
353 std::string m_windExposure;
354 double m_illuminanceSetpoint;
355 bool m_airWall;
356 std::vector<std::string> m_airLoopHVACNames;
357 std::vector<std::string> m_airLoopHVACHandles;
358 std::vector<std::string> m_airLoopHVACMaterialNames;
359 //bool m_belowFloorPlenum;
360 //bool m_aboveCeilingPlenum;
361 bool m_includeGeometryDiagnostics = false;
362 bool m_convex = true;
363 bool m_spaceConvex = true;
364 bool m_spaceEnclosed = true;
365 bool m_correctlyOriented = true;
366 };
367
368 /// ThreeSceneChild is a child object of a ThreeSceneObject
369 class UTILITIES_API ThreeSceneChild
370 {
371 public:
372 ThreeSceneChild(const std::string& uuid, const std::string& name, const std::string& type, const std::string& geometryId,
373 const std::string& materialId, const ThreeUserData& userData);
374 std::string uuid() const;
375 std::string name() const;
376 std::string type() const;
377 std::string geometry() const;
378 std::string material() const;
379 std::vector<double> matrix() const;
380 ThreeUserData userData() const;
381
382 private:
383 friend class ThreeSceneObject;
384 ThreeSceneChild(const Json::Value& value);
385 Json::Value toJsonValue() const;
386
387 std::string m_uuid;
388 std::string m_name;
389 std::string m_type;
390 std::string m_geometryId;
391 std::string m_materialId;
392 std::vector<double> m_matrix;
393 ThreeUserData m_userData;
394 };
395
396 /// ThreeSceneObject is the root object in a ThreeScene
397 class UTILITIES_API ThreeSceneObject
398 {
399 public:
400 ThreeSceneObject(const std::string& uuid, const std::vector<ThreeSceneChild>& children);
401 std::string uuid() const;
402 std::string type() const;
403 std::vector<double> matrix() const;
404 std::vector<ThreeSceneChild> children() const;
405
406 private:
407 friend class ThreeScene;
408 ThreeSceneObject(const Json::Value& value);
409 Json::Value toJsonValue() const;
410
411 std::string m_uuid;
412 std::string m_type;
413 std::vector<double> m_matrix;
414 std::vector<ThreeSceneChild> m_children;
415 };
416
417 /// ThreeBoundingBox includes information about a bounding box
418 class UTILITIES_API ThreeBoundingBox
419 {
420 public:
421 ThreeBoundingBox(double minX, double minY, double minZ, double maxX, double maxY, double maxZ, double lookAtX, double lookAtY, double lookAtZ,
422 double lookAtR);
423 double minX() const;
424 double minY() const;
425 double minZ() const;
426 double maxX() const;
427 double maxY() const;
428 double maxZ() const;
429 double lookAtX() const;
430 double lookAtY() const;
431 double lookAtZ() const;
432 double lookAtR() const;
433
434 private:
435 friend class ThreeSceneMetadata;
436 ThreeBoundingBox(const Json::Value& value);
437 Json::Value toJsonValue() const;
438
439 double m_minX;
440 double m_minY;
441 double m_minZ;
442 double m_maxX;
443 double m_maxY;
444 double m_maxZ;
445 double m_lookAtX;
446 double m_lookAtY;
447 double m_lookAtZ;
448 double m_lookAtR;
449 };
450
451 /// ThreeModelObjectMetadata includes metadata about an OpenStudio ModelObject not associated with a ThreeSceneChild in the ThreeJS scene
452 class UTILITIES_API ThreeModelObjectMetadata
453 {
454 public:
455 // DLM: public default ctor seems to be only to make SWIG happy, normal tricks of ignoring vector resize did not work
456 // additionally private default ctor with template<class _Ty> friend class std::allocator; also did not work
457 ThreeModelObjectMetadata();
458
459 ThreeModelObjectMetadata(const std::string& iddObjectType, const std::string& handle, const std::string& name);
460 std::string iddObjectType() const;
461 std::string handle() const;
462 std::string name() const;
463
464 // applies to general objects
465 std::string color() const;
466 bool setColor(const std::string& c);
467 void resetColor();
468
469 /// applies to Space
470 bool openToBelow() const;
471 bool setOpenToBelow(bool t);
472 void resetOpenToBelow();
473
474 /// applies to Story or Space
475 boost::optional<unsigned> multiplier() const;
476 bool setMultiplier(unsigned mult);
477 void resetMultiplier();
478
479 /// applies to Story
480 boost::optional<double> nominalZCoordinate() const;
481 bool setNominalZCoordinate(double z);
482 void resetNominalZCoordinate();
483
484 /// applies to Story or Space
485 boost::optional<double> belowFloorPlenumHeight() const;
486 bool setBelowFloorPlenumHeight(double height);
487 void resetBelowFloorPlenumHeight();
488
489 /// applies to Story or Space
490 boost::optional<double> floorToCeilingHeight() const;
491 bool setFloorToCeilingHeight(double height);
492 void resetFloorToCeilingHeight();
493
494 /// applies to Story or Space
495 boost::optional<double> aboveCeilingPlenumHeight() const;
496 bool setAboveCeilingPlenumHeight(double height);
497 void resetAboveCeilingPlenumHeight();
498
499 private:
500 friend class ThreeSceneMetadata;
501
502 ThreeModelObjectMetadata(const Json::Value& value);
503 Json::Value toJsonValue() const;
504
505 std::string m_iddObjectType;
506 std::string m_handle;
507 std::string m_name;
508 std::string m_color;
509
510 bool m_openToBelow;
511 boost::optional<unsigned> m_multiplier;
512 boost::optional<double> m_nominalZCoordinate;
513 boost::optional<double> m_belowFloorPlenumHeight;
514 boost::optional<double> m_floorToCeilingHeight;
515 boost::optional<double> m_aboveCeilingPlenumHeight;
516 };
517
518 /// ThreeSceneMetadata includes metadata about an OpenStudio Model Object
519 class UTILITIES_API ThreeSceneMetadata
520 {
521 public:
522 ThreeSceneMetadata(const std::vector<std::string>& buildingStoryNames, const ThreeBoundingBox& boundingBox, double northAxis,
523 const std::vector<ThreeModelObjectMetadata>& modelObjectMetadata);
524 std::string version() const;
525 std::string type() const;
526 std::string generator() const;
527 std::vector<std::string> buildingStoryNames() const;
528 ThreeBoundingBox boundingBox() const;
529 double northAxis() const;
530 std::vector<ThreeModelObjectMetadata> modelObjectMetadata() const;
531
532 private:
533 friend class ThreeScene;
534 ThreeSceneMetadata(const Json::Value& value);
535 Json::Value toJsonValue() const;
536
537 std::string m_version;
538 std::string m_type;
539 std::string m_generator;
540 std::vector<std::string> m_buildingStoryNames;
541 ThreeBoundingBox m_boundingBox;
542 double m_northAxis;
543 std::vector<ThreeModelObjectMetadata> m_modelObjectMetadata;
544 };
545
546 /** ThreeScene is an adapter for a scene in the three.js geometry format, defined at:
547 * https://github.com/mrdoob/three.js/wiki/JSON-Object-Scene-format-4
548 *
549 * The class is not impl-ized in hopes that it can be ported to JavaScript via emscripten
550 */
551 class UTILITIES_API ThreeScene
552 {
553 public:
554 /// constructor
555 ThreeScene(const ThreeSceneMetadata& metadata, const std::vector<ThreeGeometry>& geometries, const std::vector<ThreeMaterial>& materials,
556 const ThreeSceneObject& sceneObject);
557
558 /// constructor from JSON formatted string (or path to a JSON file), will throw if error
559 ThreeScene(const std::string& json_str);
560
561 /// load from string
562 static boost::optional<ThreeScene> load(const std::string& json);
563
564 /// print to JSON
565 std::string toJSON(bool prettyPrint = false) const;
566
567 ThreeSceneMetadata metadata() const;
568 std::vector<ThreeGeometry> geometries() const;
569 boost::optional<ThreeGeometry> getGeometry(const std::string& geometryId) const;
570 std::vector<ThreeMaterial> materials() const;
571 boost::optional<ThreeMaterial> getMaterial(const std::string& materialId) const;
572
573 /** This method is renamed to 'threeSceneObject()' in C# */
574 ThreeSceneObject object() const;
575
576 private:
577 − REGISTER_LOGGER("ThreeScene");
578
579 ThreeSceneMetadata m_metadata;
580 std::vector<ThreeGeometry> m_geometries;
581 std::vector<ThreeMaterial> m_materials;
582 ThreeSceneObject m_sceneObject;
583 };
584
585 } // namespace openstudio
586
587 #endif //UTILITIES_GEOMETRY_THREEJS_HPP
588