Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polygon and Triangle Support #44

Merged
merged 9 commits into from
Oct 31, 2023
4 changes: 2 additions & 2 deletions libs/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ project(erdblick-core)

# For WASM modules, add_executable is used instead of add_library.
set(ERDBLICK_SOURCE_FILES
include/erdblick/renderer.h
include/erdblick/visualization.h
include/erdblick/style.h
include/erdblick/rule.h
include/erdblick/buffer.h
Expand All @@ -16,7 +16,7 @@ set(ERDBLICK_SOURCE_FILES
include/erdblick/cesium-interface/cesium.h
include/erdblick/cesium-interface/point-conversion.h

src/renderer.cpp
src/visualization.cpp
src/style.cpp
src/rule.cpp
src/color.cpp
Expand Down
9 changes: 8 additions & 1 deletion libs/core/include/erdblick/cesium-interface/cesium.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@ namespace erdblick
struct CesiumLib
{
CesiumClass ArcType;
CesiumClass BoundingSphere;
CesiumClass Color;
CesiumClass ColorGeometryInstanceAttribute;
CesiumClass ComponentDatatype;
CesiumClass Geometry;
CesiumClass GeometryAttribute;
CesiumClass GeometryInstance;
CesiumClass Material;
CesiumClass PerInstanceColorAppearance;
CesiumClass PolygonGeometry;
CesiumClass PolygonHierarchy;
CesiumClass PolylineColorAppearance;
CesiumClass PolylineGeometry;
CesiumClass PolylineMaterialAppearance;
CesiumClass Primitive;
CesiumClass PrimitiveCollection;
CesiumClass PrimitiveType;

[[nodiscard]] JsValue MaterialFromType(std::string const& type, JsValue const& options);
[[nodiscard]] JsValue ColorAttributeFromColor(JsValue const& color);

private:
friend CesiumLib& Cesium();
Expand Down
12 changes: 9 additions & 3 deletions libs/core/include/erdblick/cesium-interface/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ struct JsValue
* Construct an Object as a new JS or JSON dictionary with provided initializers.
* @param initializers An initializer list of key-value pairs.
*/
static JsValue
newDict(std::initializer_list<std::pair<std::string, JsValue>> initializers = {});
static JsValue Dict(std::initializer_list<std::pair<std::string, JsValue>> initializers = {});

/**
* Construct an Object as a new JS or JSON list with provided initializers.
* @param initializers An initializer list of CesiumObject items.
*/
static JsValue newList(std::initializer_list<JsValue> initializers = {});
static JsValue List(std::initializer_list<JsValue> initializers = {});

/**
* Construct an Object as a new JS Float64 TypedArray.
* @param coordinates Float64 buffer to fill the typed array.
*/
static JsValue Float64Array(std::vector<double> const& coordinates);

/**
* Constructs a JavaScript or JSON null value.
Expand Down Expand Up @@ -122,6 +127,7 @@ struct CesiumClass : public JsValue
* For EMSCRIPTEN, it utilizes value_.new_(Args...).
* For the mock version, it will return an empty nlohmann JSON object.
*/
JsValue New(std::initializer_list<std::pair<std::string, JsValue>> kwArgs = {}) const;
template<typename... Args>
JsValue New(Args... args) const;

Expand Down
62 changes: 56 additions & 6 deletions libs/core/include/erdblick/cesium-interface/primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "object.h"
#include "mapget/model/tileid.h"
#include "rule.h"
#include "../rule.h"

namespace erdblick
{
Expand All @@ -24,22 +24,72 @@ struct CesiumPrimitive
static CesiumPrimitive withPolylineColorAppearance();

/**
* Add a 3D polyline to the primitive. The provided coordinates
* must already be transformed to Cesium cartesian coordinates.
* Create a primitive which uses the PerInstanceColorAppearance.
* See https://cesium.com/learn/cesiumjs/ref-doc/PerInstanceColorAppearance.html
*
* The parameter flatAndSynchronous must be set to true for primitives
* which contain basic triangle meshes. In the future, we can also have
* smoothly shaded triangle meshes by calling Cesium.GeometryPipeline.computeNormal
* and Cesium.GeometryPipeline.compressVertices on the mesh geometry.
*/
void addLine(JsValue const& pointList, FeatureStyleRule const& style, uint32_t id);
static CesiumPrimitive withPerInstanceColorAppearance(bool flatAndSynchronous = false);

/**
* Add a 3D polyline to the primitive. The provided vertices
* must be a JS list of Point objects in Cesium cartesian coordinates.
*
* Note: In order to visualize the line correctly, the primitive
* must have been constructed using withPolylineColorAppearance.
*/
void addPolyLine(JsValue const& vertices, FeatureStyleRule const& style, uint32_t id);

/**
* Add a 3D polygon to the primitive. The provided vertices
* must be a JS list of Point objects in Cesium cartesian coordinates.
*
* Note: In order to visualize the polygon correctly, the primitive
* must have been constructed using withPerInstanceColorAppearance.
*/
void addPolygon(JsValue const& vertices, FeatureStyleRule const& style, uint32_t id);

/**
* Add a 3D triangle mesh to the primitive. The provided vertices
* must be a JS Float64Array like [x0,y0,z0,x1,y1,z2...]. This is unlike other functions
* here which need a JS list of Point objects, due to Cesium internals.
*
* Note: In order to visualize the triangles correctly, the primitive
* must have been constructed using withPerInstanceColorAppearance(true).
*/
void addTriangles(JsValue const& float64Array, FeatureStyleRule const& style, uint32_t id);

/**
* Constructs a JS Primitive from the provided Geometry instances.
*/
NativeJsValue toJsObject();
[[nodiscard]] NativeJsValue toJsObject() const;

/**
* Check if any geometry has been added to the primitive.
*/
[[nodiscard]] bool empty() const;

private:
/**
* Add a Cesium GeometryInstance which wraps a Cesium Geometry,
* and add it to this primitive's geometryInstances_ collection.
*/
void addGeometryInstance(const FeatureStyleRule& style, uint32_t id, const JsValue& geom);

/** Number of entries in geometryInstances_. */
size_t numGeometryInstances_ = 0;

/** geometryInstances option for the Primitive JS Object ctor. */
JsValue geometryInstances_ = JsValue::newList();
JsValue geometryInstances_ = JsValue::List();

/** appearance option for the Primitive JS Object ctor. */
JsValue appearance_;

/** Flag which enables the direct triangle display required for addTriangles. */
bool flatAndSynchronous_ = false;
};

}
24 changes: 0 additions & 24 deletions libs/core/include/erdblick/renderer.h

This file was deleted.

16 changes: 10 additions & 6 deletions libs/core/include/erdblick/rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ namespace erdblick
class FeatureStyleRule
{
public:
FeatureStyleRule(YAML::Node const& yaml);
bool match(mapget::Feature& feature) const;
explicit FeatureStyleRule(YAML::Node const& yaml);

const std::vector<simfil::Geometry::GeomType>& geometryTypes() const;
glm::fvec4 const& color() const;
float width() const;
[[nodiscard]] bool match(mapget::Feature& feature) const;
[[nodiscard]] bool supports(mapget::Geometry::GeomType const& g) const;
[[nodiscard]] glm::fvec4 const& color() const;
[[nodiscard]] float width() const;

private:
std::vector<simfil::Geometry::GeomType> geometryTypes_;
static inline uint32_t geomTypeBit(mapget::Geometry::GeomType const& g) {
return 1 << static_cast<std::underlying_type_t<mapget::Geometry::GeomType>>(g);
}

uint32_t geometryTypes_ = 0; // bitfield from GeomType enum
std::optional<std::regex> type_;
std::string filter_;
glm::fvec4 color_{.0, .0, .0, 1.};
Expand Down
Loading