Skip to content

Commit

Permalink
Adjust to new validity API.
Browse files Browse the repository at this point in the history
  • Loading branch information
josephbirkner committed Nov 11, 2024
1 parent b206919 commit ec72bea
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 119 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ message("Building for ${CMAKE_SYSTEM_NAME}.")

FetchContent_Declare(mapget
GIT_REPOSITORY "https://github.com/Klebert-Engineering/mapget"
GIT_TAG "v2024.4.1"
# Version with new validity model
GIT_TAG "5dbf63e30"
GIT_SHALLOW ON)
FetchContent_MakeAvailable(mapget)

Expand Down
11 changes: 3 additions & 8 deletions libs/core/include/erdblick/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,21 @@ bool isPointInsideTriangle(m::Point const& p, m::Point const& p0, m::Point const
* This is used as a location for labels, and as the origin
* for relation vectors.
*/
m::Point geometryCenter(m::model_ptr<m::Geometry> const& g);
m::Point geometryCenter(m::SelfContainedGeometry const& g);

/**
* Calculate a point furthest from the center for the given geometry.
* Used to properly scale the camera in the viewer
* relative to the feature's bounding sphere.
*/
m::Point boundingRadiusEndPoint(m::model_ptr<m::Geometry> const& g);

/**
* Get type of the geometry.
*/
m::GeomType getGeometryType(m::model_ptr<m::Geometry> const& g);
m::Point boundingRadiusEndPoint(m::SelfContainedGeometry const& g);

/**
* Calculate a local WGS84 coordinate system for the geometry.
* The axes are scaled, such that each represents approx. 1m
* in real-world length. The y-axis will point in the direction
* (first-point -> last-point). The x-axis is perpendicular.
*/
glm::dmat3x3 localWgs84UnitCoordinateSystem(const m::model_ptr<m::Geometry>& g);
glm::dmat3x3 localWgs84UnitCoordinateSystem(mapget::SelfContainedGeometry const& g);

} // namespace erdblick
1 change: 1 addition & 0 deletions libs/core/include/erdblick/inspection.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class InspectionConverter
void convertAttributeLayer(std::string_view const& name, mapget::model_ptr<mapget::AttributeLayer> const& l);
void convertRelation(mapget::model_ptr<mapget::Relation> const& r);
void convertGeometry(JsValue const& key, mapget::model_ptr<mapget::Geometry> const& r);
void convertValidity(JsValue const& key, mapget::model_ptr<mapget::MultiValidity> const& r);

OptionalValueAndType convertField(simfil::StringId const& fieldId, simfil::ModelNode::Ptr const& value);
OptionalValueAndType convertField(std::string_view const& fieldName, simfil::ModelNode::Ptr const& value);
Expand Down
2 changes: 1 addition & 1 deletion libs/core/include/erdblick/testdataprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class TestDataProvider
// Add an attribute layer
auto attrLayer = feature->attributeLayers()->newLayer("lane");
auto attr = attrLayer->newAttribute("numLanes");
attr->setDirection(mapget::Attribute::Direction::Positive);
attr->validity()->newDirection(mapget::Validity::Positive);
attr->addField("count", (int64_t)rand());
}

Expand Down
7 changes: 7 additions & 0 deletions libs/core/include/erdblick/visualization.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ class FeatureLayerVisualization
* Add some geometry. The Cesium conversion will be dispatched,
* based on the geometry type and the style rule instructions.
*/
void addGeometry(
mapget::SelfContainedGeometry const& geom,
std::string_view id,
FeatureStyleRule const& rule,
std::string const& mapLayerStyleRuleId,
BoundEvalFun& evalFun,
glm::dvec3 const& offset = {.0, .0, .0});
void addGeometry(
mapget::model_ptr<mapget::Geometry> const& geom,
std::string_view id,
Expand Down
2 changes: 1 addition & 1 deletion libs/core/src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ EMSCRIPTEN_BINDINGS(erdblick)
"getGeometryType",
std::function<mapget::GeomType(FeaturePtr&)>(
[](FeaturePtr& self){
return getGeometryType(self->firstGeometry());
return self->firstGeometry().geomType_;
}));

////////// GeomType
Expand Down
3 changes: 3 additions & 0 deletions libs/core/src/cesium-interface/billboards.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ JsValue CesiumBillboardCollection::billboardParams(
const BoundEvalFun& evalFun)
{
auto result = CesiumPointPrimitiveCollection::pointParams(position, style, id, evalFun);
// TODO: Support non-square icons.
result.set("width", JsValue(style.width()));
result.set("height", JsValue(style.width()));
if (style.hasIconUrl()) {
result.set("image", JsValue(style.iconUrl(evalFun)));
}
Expand Down
53 changes: 16 additions & 37 deletions libs/core/src/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@

using namespace mapget;

Point erdblick::geometryCenter(const model_ptr<Geometry>& g)
Point erdblick::geometryCenter(const SelfContainedGeometry& g)
{
if (!g) {
std::cerr << "Cannot obtain center of null geometry." << std::endl;
return {};
}

// Initialize variables for averaging.
uint32_t totalPoints = g->numPoints();
std::vector<mapget::Point> points;
points.reserve(g->numPoints());
auto totalPoints = g.points_.size();
auto const& points = g.points_;

// Lambda to update totalX, totalY, totalZ, and count.
auto averageVectorPosition = [](const std::vector<Point>& points)
Expand All @@ -32,21 +26,13 @@ Point erdblick::geometryCenter(const model_ptr<Geometry>& g)
return result;
};

// Process all points to find the average position.
g->forEachPoint(
[&points](const auto& p)
{
points.push_back(p);
return true; // Continue iterating.
});

if (totalPoints == 0) {
std::cerr << "Geometry has no points." << std::endl;
return {};
}

Point averagePoint = averageVectorPosition(points);
if (g->geomType() != GeomType::Mesh && g->geomType() != GeomType::Line) {
if (g.geomType_ != GeomType::Mesh && g.geomType_ != GeomType::Line) {
return averagePoint;
}

Expand All @@ -65,7 +51,7 @@ Point erdblick::geometryCenter(const model_ptr<Geometry>& g)
});

// For lines, return the shape-point closest to the average.
if (g->geomType() == GeomType::Line) {
if (g.geomType_ == GeomType::Line) {
if (totalPoints % 2 == 1) {
// Odd number of points: Return closest point.
return pointsSorted.front();
Expand Down Expand Up @@ -120,36 +106,29 @@ Point erdblick::geometryCenter(const model_ptr<Geometry>& g)
return averageVectorPosition(intersectedTrianglePoints);
}

Point erdblick::boundingRadiusEndPoint(const model_ptr<Geometry>& g)
Point erdblick::boundingRadiusEndPoint(const SelfContainedGeometry& g)
{
const Point center = erdblick::geometryCenter(g);
if (!g) {
const Point center = geometryCenter(g);
if (g.points_.empty()) {
std::cerr << "Cannot obtain bounding radius vector end point of null geometry." << std::endl;
return center;
}

float maxDistanceSquared = 0.0f;
Point farPoint = center;
g->forEachPoint([&center, &maxDistanceSquared, &farPoint](const auto& p)
for (auto const& p : g.points_)
{
float dx = p.x - center.x;
float dy = p.y - center.y;
float dz = p.z - center.z;
float distanceSquared = dx * dx + dy * dy + dz * dz;
auto d = p - center;
float distanceSquared = d.x * d.x + d.y * d.y + d.z * d.z;
if (distanceSquared > maxDistanceSquared) {
farPoint = p;
maxDistanceSquared = distanceSquared;
}
return true;
});
}

return farPoint;
}

GeomType erdblick::getGeometryType(const model_ptr<Geometry>& g) {
return g->geomType();
}

double erdblick::pointSideOfLine(const Point& lineVector, const Point& lineStart, const Point& p)
{
return lineVector.x * (p.y - lineStart.y) - lineVector.y * (p.x - lineStart.x);
Expand Down Expand Up @@ -198,7 +177,7 @@ bool erdblick::isPointInsideTriangle(
return (side0 <= 0 && side1 <= 0 && side2 <= 0) || (side0 >= 0 && side1 >= 0 && side2 >= 0);
}

glm::dmat3x3 erdblick::localWgs84UnitCoordinateSystem(const model_ptr<Geometry>& g)
glm::dmat3x3 erdblick::localWgs84UnitCoordinateSystem(const SelfContainedGeometry& g)
{
constexpr auto latMetersPerDegree = 110574.; // Meters per degree of latitude
constexpr auto lonMetersPerDegree = 111320.; // Meters per degree of longitude at equator
Expand All @@ -207,13 +186,13 @@ glm::dmat3x3 erdblick::localWgs84UnitCoordinateSystem(const model_ptr<Geometry>&
{.0, 1./latMetersPerDegree, .0},
{.0, .0, 1.}};

if (!g || g->geomType() != GeomType::Line || g->numPoints() < 2) {
if (g.geomType_ != GeomType::Line || g.points_.size() < 2) {
return defaultResult;
}

auto const aWgs = g->pointAt(0);
auto const aWgs = g.points_[0];
auto const a = wgsToCartesian<glm::dvec3>(aWgs);
auto const b = wgsToCartesian<glm::dvec3>(g->pointAt(g->numPoints() - 1));
auto const b = wgsToCartesian<glm::dvec3>(g.points_.back());
auto const c = wgsToCartesian<glm::dvec3>(aWgs, {.0, .0, 1.});
auto const forward = glm::normalize(b - a);
auto const up = glm::normalize(c - a);
Expand Down
Loading

0 comments on commit ec72bea

Please sign in to comment.