src/utilities/geometry/FloorplanJS.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_FLOORPLANJS_HPP | ||
| 7 | #define UTILITIES_GEOMETRY_FLOORPLANJS_HPP | ||
| 8 | |||
| 9 | #include "../UtilitiesAPI.hpp" | ||
| 10 | |||
| 11 | #include "Point3d.hpp" | ||
| 12 | #include "Transformation.hpp" | ||
| 13 | #include "BoundingBox.hpp" | ||
| 14 | |||
| 15 | #include "../core/Logger.hpp" | ||
| 16 | #include "../core/UUID.hpp" | ||
| 17 | |||
| 18 | #include <json/json.h> | ||
| 19 | |||
| 20 | #include <vector> | ||
| 21 | #include <set> | ||
| 22 | #include <boost/optional.hpp> | ||
| 23 | |||
| 24 | namespace openstudio { | ||
| 25 | |||
| 26 | class ThreeScene; | ||
| 27 | class ThreeSceneChild; | ||
| 28 | class ThreeGeometry; | ||
| 29 | class ThreeModelObjectMetadata; | ||
| 30 | class ThreeMaterial; | ||
| 31 | class FloorplanObjectId; | ||
| 32 | class Point3d; | ||
| 33 | class FloorplanJS; | ||
| 34 | |||
| 35 | /** FloorplanObject is used to update data in a FloorplanJS object | ||
| 36 | * | ||
| 37 | * The class is not impl-ized in hopes that it can be ported to JavaScript via emscripten | ||
| 38 | */ | ||
| 39 | class UTILITIES_API FloorplanObject | ||
| 40 | { | ||
| 41 | public: | ||
| 42 | /// pass empty string for any null values | ||
| 43 | FloorplanObject(const std::string& id, const std::string& name, const std::string& handleString); | ||
| 44 | FloorplanObject(const std::string& id, const std::string& name, const UUID& handle); | ||
| 45 | |||
| 46 | /// id in FloorplanJS | ||
| 47 | std::string id() const; | ||
| 48 | |||
| 49 | /// object name | ||
| 50 | std::string name() const; | ||
| 51 | |||
| 52 | /// handle of corresponding OpenStudio Model object, will be null if none | ||
| 53 | UUID handle() const; | ||
| 54 | |||
| 55 | /// handle of corresponding OpenStudio Model object, will be empty string if none | ||
| 56 | std::string handleString() const; | ||
| 57 | |||
| 58 | /// handle of corresponding OpenStudio Model parent object, will be null if none | ||
| 59 | boost::optional<std::string> parentHandleString() const; | ||
| 60 | void setParentHandleString(const std::string& parentHandleString); | ||
| 61 | void resetParentHandleString(); | ||
| 62 | |||
| 63 | boost::optional<double> getDataDouble(const std::string& key) const; | ||
| 64 | boost::optional<int> getDataInt(const std::string& key) const; | ||
| 65 | boost::optional<bool> getDataBool(const std::string& key) const; // DLM: warning, optional bool! | ||
| 66 | boost::optional<std::string> getDataString(const std::string& key) const; | ||
| 67 | boost::optional<FloorplanObject> getDataReference(const std::string& key) const; | ||
| 68 | |||
| 69 | void setDataDouble(const std::string& key, double value); | ||
| 70 | void setDataInt(const std::string& key, int value); | ||
| 71 | void setDataBool(const std::string& key, bool value); | ||
| 72 | void setDataString(const std::string& key, const std::string& value); | ||
| 73 | void setDataReference(const std::string& key, const FloorplanObject& value); | ||
| 74 | |||
| 75 | private: | ||
| 76 | friend class FloorplanJS; | ||
| 77 | |||
| 78 | FloorplanObject(const Json::Value& value); | ||
| 79 | |||
| 80 | Json::Value data() const; | ||
| 81 | |||
| 82 | std::map<std::string, FloorplanObject> objectReferenceMap() const; | ||
| 83 | |||
| 84 | std::string m_id; | ||
| 85 | std::string m_name; | ||
| 86 | UUID m_handle; | ||
| 87 | std::string m_handleString; | ||
| 88 | boost::optional<std::string> m_parentHandleString; | ||
| 89 | |||
| 90 | Json::Value m_data; | ||
| 91 | |||
| 92 | std::map<std::string, FloorplanObject> m_objectReferenceMap; | ||
| 93 | }; | ||
| 94 | |||
| 95 | /** FloorplanJS is an adapter for the FloorspaceJS JSON format. This class includes code which transforms a FloorspaceJS JSON into a 3D model in ThreeJS format. | ||
| 96 | * There are two variations of the ThreeJS format, one which is suitable for rendering with ThreeJS and one that preserves all vertices in a surface for | ||
| 97 | * conversion to OpenStudio Model format. Converting from FloorspaceJS to ThreeJS to OpenStudio ensures that the ThreeJS preview of a FloorspaceJS model is as | ||
| 98 | * accurate as possible. These conversions can also be used to convert a FloorspaceJS file to a 3D model for translation to other 3D formats. | ||
| 99 | * | ||
| 100 | * FloorplanJS does not support version translation of old FloorspaceJS files, that functionality is in the FloorspaceJS project. | ||
| 101 | * | ||
| 102 | * The ThreeJSForwardTranslator in the Model project converts an OpenStudio Model to ThreeJS format, code should be shared between these two classes as much as possible. | ||
| 103 | * | ||
| 104 | * The class is not impl-ized in hopes that it can be ported to JavaScript via emscripten | ||
| 105 | */ | ||
| 106 | class UTILITIES_API FloorplanJS | ||
| 107 | { | ||
| 108 | public: | ||
| 109 | /// default constructor | ||
| 110 | FloorplanJS(); | ||
| 111 | |||
| 112 | /// constructor from JSON formatted string, will throw if error | ||
| 113 | FloorplanJS(const std::string& json); | ||
| 114 | |||
| 115 | /// load from string | ||
| 116 | static boost::optional<FloorplanJS> load(const std::string& json); | ||
| 117 | |||
| 118 | /// print to JSON | ||
| 119 | std::string toJSON(bool prettyPrint = false) const; | ||
| 120 | |||
| 121 | /// convert to ThreeJS, will throw if error | ||
| 122 | /// if openstudioFormat is true, surfaces will be created with arbitrary number of vertices, not suitable for rendering using ThreeJS | ||
| 123 | /// if openstudioFormat is false, surfaces will be created with with either 3 or 4 vertices, suitable for rendering using ThreeJS | ||
| 124 | /// ThreeJS file produced will always be in metric units, NorthAxis will not be applied during this conversion | ||
| 125 | ThreeScene toThreeScene(bool openstudioFormat) const; | ||
| 126 | |||
| 127 | /// unit system, "ip" or "si" | ||
| 128 | std::string units() const; | ||
| 129 | bool setUnits(const std::string& units); | ||
| 130 | void resetUnits(); | ||
| 131 | |||
| 132 | /// degrees from Building North to True North measured clockwise, opposite of EnergyPlus convention | ||
| 133 | double northAxis() const; | ||
| 134 | bool setNorthAxis(double northAxis); | ||
| 135 | void resetNorthAxis(); | ||
| 136 | |||
| 137 | /// degrees, North is positive, South is negative | ||
| 138 | double latitude() const; | ||
| 139 | bool setLatitude(double latitude); | ||
| 140 | void resetLatitude(); | ||
| 141 | |||
| 142 | /// degrees, West is negative, East is positive | ||
| 143 | double longitude() const; | ||
| 144 | bool setLongitude(double longitude); | ||
| 145 | void resetLongitude(); | ||
| 146 | |||
| 147 | /// elevation from sea level | ||
| 148 | double elevation() const; | ||
| 149 | bool setElevation(double elevation); | ||
| 150 | void resetElevation(); | ||
| 151 | |||
| 152 | /// update object names in Floorplan with external data | ||
| 153 | /// if object with same handle exists, name will be updated | ||
| 154 | /// else if object with same name exists, handle will be assigned | ||
| 155 | /// else new object will be added | ||
| 156 | /// if removeMissingObjects is true, then existing objects not in input will be deleted | ||
| 157 | void updateStories(const std::vector<FloorplanObject>& objects, bool removeMissingObjects); | ||
| 158 | void updateSpaces(const std::vector<FloorplanObject>& objects, bool removeMissingObjects); | ||
| 159 | void updateBuildingUnits(const std::vector<FloorplanObject>& objects, bool removeMissingObjects); | ||
| 160 | void updateThermalZones(const std::vector<FloorplanObject>& objects, bool removeMissingObjects); | ||
| 161 | void updateSpaceTypes(const std::vector<FloorplanObject>& objects, bool removeMissingObjects); | ||
| 162 | void updateConstructionSets(const std::vector<FloorplanObject>& objects, bool removeMissingObjects); | ||
| 163 | |||
| 164 | private: | ||
| 165 | − | REGISTER_LOGGER("FloorplanJS"); | |
| 166 | |||
| 167 | FloorplanJS(const Json::Value& value); | ||
| 168 | |||
| 169 | static ThreeModelObjectMetadata makeModelObjectMetadata(const std::string& iddObjectType, const Json::Value& object); | ||
| 170 | |||
| 171 | void makeGeometries(const Json::Value& story, const Json::Value& spaceOrShading, bool belowFloorPlenum, bool aboveCeilingPlenum, | ||
| 172 | double lengthToMeters, double minZ, double maxZ, const Json::Value& vertices, const Json::Value& edges, | ||
| 173 | const Json::Value& faces, const std::string& faceId, bool openstudioFormat, std::vector<ThreeGeometry>& geometries, | ||
| 174 | std::vector<ThreeSceneChild>& sceneChildren, bool openToBelow) const; | ||
| 175 | |||
| 176 | std::string makeSurface(const Json::Value& story, const Json::Value& spaceOrShading, const std::string& parentSurfaceName, | ||
| 177 | const std::string& parentSubSurfaceName, bool belowFloorPlenum, bool aboveCeilingPlenum, const std::string& surfaceType, | ||
| 178 | const Point3dVectorVector& finalFaceVertices, size_t faceFormat, std::vector<ThreeGeometry>& geometries, | ||
| 179 | std::vector<ThreeSceneChild>& sceneChildren, double illuminanceSetpoint, bool airWall) const; | ||
| 180 | |||
| 181 | static void makeMaterial(const Json::Value& object, const std::string& iddObjectType, std::vector<ThreeMaterial>& materials, | ||
| 182 | std::map<std::string, std::string>& materialMap); | ||
| 183 | |||
| 184 | static std::string getHandleString(const Json::Value& value); | ||
| 185 | static std::string getName(const Json::Value& value); | ||
| 186 | static std::string getId(const Json::Value& value); | ||
| 187 | static std::string getFaceId(const Json::Value& value); | ||
| 188 | |||
| 189 | std::string getNextId(); | ||
| 190 | |||
| 191 | // recursively traverses through value and finds the largest id | ||
| 192 | void setLastId(const Json::Value& value); | ||
| 193 | |||
| 194 | static Json::Value* findByHandleString(Json::Value& value, const std::string& key, const std::string& handleString); | ||
| 195 | static Json::Value* findByName(Json::Value& value, const std::string& key, const std::string& name, bool requireEmptyHandle); | ||
| 196 | static Json::Value* findById(Json::Value& value, const std::string& key, const std::string& id); | ||
| 197 | |||
| 198 | // cppcheck-suppress functionStatic | ||
| 199 | const Json::Value* findById(const Json::Value& values, const std::string& id) const; | ||
| 200 | |||
| 201 | void updateObjects(Json::Value& value, const std::string& key, const std::vector<FloorplanObject>& objects, bool removeMissingObjects); | ||
| 202 | void updateObjectReference(Json::Value& value, const std::string& key, const FloorplanObject& objectReference, bool removeMissingObjects); | ||
| 203 | |||
| 204 | void removeFaces(Json::Value& value, const std::set<std::string>& faceIdsToRemove); | ||
| 205 | void removeEdges(Json::Value& value, const std::set<std::string>& edgeIdsToRemove); | ||
| 206 | // cppcheck-suppress functionStatic | ||
| 207 | void removeVertices(Json::Value& value, const std::set<std::string>& vertexIdsToRemove); | ||
| 208 | |||
| 209 | Json::Value m_value; | ||
| 210 | |||
| 211 | unsigned m_lastId; | ||
| 212 | mutable std::set<std::string> m_plenumThermalZoneNames; | ||
| 213 | mutable BoundingBox m_boundingBox; | ||
| 214 | }; | ||
| 215 | |||
| 216 | /// convienence method, converts a FloorplanJS JSON string to a ThreeJS JSON string | ||
| 217 | UTILITIES_API std::string floorplanToThreeJS(const std::string& json, bool openstudioFormat); | ||
| 218 | |||
| 219 | } // namespace openstudio | ||
| 220 | |||
| 221 | #endif //UTILITIES_GEOMETRY_FLOORPLANJS_HPP | ||
| 222 |