Skip to content

Commit

Permalink
Geometry preview working
Browse files Browse the repository at this point in the history
  • Loading branch information
macumber committed Apr 12, 2017
1 parent 8dc72b6 commit e37e6d7
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 30 deletions.
3 changes: 3 additions & 0 deletions openstudiocore/src/model/ModelCore.i
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@


%{
#include <model/ThreeJS.hpp>

#include <utilities/data/TimeSeries.hpp>
#include <utilities/sql/SqlFile.hpp>
#include <utilities/geometry/ThreeJS.hpp>

#include <utilities/units/Quantity.hpp>
#include <utilities/units/Unit.hpp>
Expand Down
42 changes: 28 additions & 14 deletions openstudiocore/src/model/ThreeJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ namespace openstudio
}
}

void makeGeometries(const PlanarSurface& planarSurface, std::vector<ThreeGeometry>& geometries, std::vector<ThreeUserData>& userDatas)
void makeGeometries(const PlanarSurface& planarSurface, std::vector<ThreeGeometry>& geometries, std::vector<ThreeUserData>& userDatas, bool triangulateSurfaces)
{
std::string name = planarSurface.nameString();
boost::optional<Surface> surface = planarSurface.optionalCast<Surface>();
Expand All @@ -417,26 +417,40 @@ namespace openstudio
}
}

// triangulate surface
Point3dVectorVector finalFaceVertices = computeTriangulation(faceVertices, faceSubVertices);
if (finalFaceVertices.empty()){
LOG_FREE(Error, "modelToThreeJS", "Failed to triangulate surface " << name << " with " << faceSubVertices.size() << " sub surfaces");
return;
Point3dVectorVector finalFaceVertices;
if (triangulateSurfaces){
finalFaceVertices = computeTriangulation(faceVertices, faceSubVertices);
if (finalFaceVertices.empty()){
LOG_FREE(Error, "modelToThreeJS", "Failed to triangulate surface " << name << " with " << faceSubVertices.size() << " sub surfaces");
return;
}
} else{
finalFaceVertices.push_back(faceVertices);
}

Point3dVector allVertices;
std::vector<size_t> faceIndices;
for (const auto& finalFaceTriangle : finalFaceVertices) {
Point3dVector finalTriangle = siteTransformation*t*finalFaceTriangle;
for (const auto& finalFaceVerts : finalFaceVertices) {
Point3dVector finalVerts = siteTransformation*t*finalFaceVerts;
//normal = siteTransformation.rotationMatrix*r*z

// https://github.com/mrdoob/three.js/wiki/JSON-Model-format-3
// 0 indicates triangle
// 16 indicates triangle with normals
faceIndices.push_back(0);
// 1 indicates quad
// 2 indicates triangle with material
// 3 indicates quad with material
// ....
// 255 quad with everything
// 1024 - OpenStudio format, all vertices belong to single face

if (triangulateSurfaces){
faceIndices.push_back(0);
} else{
faceIndices.push_back(1024);
}

Point3dVector::reverse_iterator it = finalTriangle.rbegin();
Point3dVector::reverse_iterator itend = finalTriangle.rend();
Point3dVector::reverse_iterator it = finalVerts.rbegin();
Point3dVector::reverse_iterator itend = finalVerts.rend();
for (; it != itend; ++it){
faceIndices.push_back(getVertexIndex(*it, allVertices));
}
Expand Down Expand Up @@ -477,7 +491,7 @@ namespace openstudio
userDatas.push_back(userData);
}

ThreeScene modelToThreeJS(Model model)
ThreeScene modelToThreeJS(Model model, bool triangulateSurfaces)
{
std::vector<ThreeMaterial> materials;
std::map<std::string, std::string> materialMap;
Expand All @@ -491,7 +505,7 @@ namespace openstudio
{
std::vector<ThreeGeometry> geometries;
std::vector<ThreeUserData> userDatas;
makeGeometries(planarSurface, geometries, userDatas);
makeGeometries(planarSurface, geometries, userDatas, triangulateSurfaces);
OS_ASSERT(geometries.size() == userDatas.size());

size_t n = geometries.size();
Expand Down
5 changes: 4 additions & 1 deletion openstudiocore/src/model/ThreeJS.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ namespace openstudio
{
namespace model
{
MODEL_API ThreeScene modelToThreeJS(Model model);
/// Convert an OpenStudio Model to ThreeJS format
/// Triangulate surfaces if the ThreeJS representation will be used for display
/// Do not triangulate surfaces if the ThreeJs representation will be translated back to a model
MODEL_API ThreeScene modelToThreeJS(Model model, bool triangulateSurfaces);

MODEL_API boost::optional<Model> modelFromThreeJS(const ThreeScene& scene);

Expand Down
8 changes: 7 additions & 1 deletion openstudiocore/src/model/test/ThreeJS_GTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ using namespace openstudio::model;

TEST_F(ModelFixture,ThreeJS_ExampleModel) {
Model model = exampleModel();
ThreeScene scene = modelToThreeJS(model);

// triangulated, for display
ThreeScene scene = modelToThreeJS(model, true);
std::string json = scene.toJSON();

// triangulated, for model transport/translation
scene = modelToThreeJS(model, false);
json = scene.toJSON();

boost::optional<Model> model2 = modelFromThreeJS(scene);
}

19 changes: 8 additions & 11 deletions openstudiocore/src/openstudio_lib/GeometryPreviewView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "GeometryPreviewView.hpp"

#include "../model/Model_Impl.hpp"
#include "../model/ThreeJS.hpp"

#include "../utilities/core/Assert.hpp"

Expand All @@ -55,7 +56,7 @@ GeometryPreviewView::GeometryPreviewView(bool isIP,

QVBoxLayout *layout = new QVBoxLayout;

PreviewWebView* webView = new PreviewWebView(this);
PreviewWebView* webView = new PreviewWebView(model, this);
layout->addWidget(webView);

setLayout(layout);
Expand All @@ -66,8 +67,9 @@ GeometryPreviewView::~GeometryPreviewView()

}

PreviewWebView::PreviewWebView(QWidget *t_parent)
PreviewWebView::PreviewWebView(const model::Model& model, QWidget *t_parent)
: QWidget(t_parent),
m_model(model),
m_isIP(true),
m_progressBar(new QProgressBar()),
m_refreshBtn(new QPushButton("Refresh"))
Expand Down Expand Up @@ -147,16 +149,11 @@ void PreviewWebView::onLoadFinished(bool ok)
m_progressBar->setTextVisible(true);
}

ThreeScene scene = modelToThreeJS(m_model, true);
std::string json = scene.toJSON(false);

// call init and animate
QString jsonFile;
QFile inFile(QString(":/library/example_geometry.json"));
if (inFile.open(QFile::ReadOnly)){
QTextStream docIn(&inFile);
jsonFile = docIn.readAll();
inFile.close();
}

QString javascript = QString("init(") + jsonFile + QString(");\n animate();\n initDatGui();");
QString javascript = QString("init(") + QString::fromStdString(json) + QString(");\n animate();\n initDatGui();");
m_view->page()->runJavaScript(javascript);

//javascript = QString("os_data.metadata.version");
Expand Down
3 changes: 2 additions & 1 deletion openstudiocore/src/openstudio_lib/GeometryPreviewView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class PreviewWebView : public QWidget
Q_OBJECT;

public:
PreviewWebView(QWidget *t_parent = nullptr);
PreviewWebView(const openstudio::model::Model& model, QWidget *t_parent = nullptr);
virtual ~PreviewWebView();

public slots:
Expand All @@ -84,6 +84,7 @@ class PreviewWebView : public QWidget
REGISTER_LOGGER("openstudio::PreviewWebView");

bool m_isIP;
model::Model m_model;

QProgressBar * m_progressBar;
QPushButton * m_refreshBtn;
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion openstudiocore/src/openstudio_lib/openstudio.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,6 @@
<file>library/tid_api.xml</file>
<file>library/geometry_editor.html</file>
<file>library/geometry_preview.html</file>
<file>library/example_geometry.json</file>


<file alias="shared_gui_components/images/add_script_icon.png">../shared_gui_components/images/add_script_icon.png</file>
Expand Down
1 change: 1 addition & 0 deletions openstudiocore/src/utilities/geometry/Geometry.i
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
%include <utilities/geometry/Transformation.hpp>
%include <utilities/geometry/BoundingBox.hpp>
%include <utilities/geometry/Intersection.hpp>
%include <utilities/geometry/ThreeJS.hpp>

%extend openstudio::Vector3d{
std::string __str__() const {
Expand Down

0 comments on commit e37e6d7

Please sign in to comment.