【c++】Lanelet2 Examples笔记(一)
01_dealing_with_lanelet_primitives
#include <lanelet2_core/LaneletMap.h>
#include <lanelet2_core/geometry/Area.h>
#include <lanelet2_core/geometry/Lanelet.h>
#include <lanelet2_core/primitives/Area.h>
#include <lanelet2_core/primitives/Lanelet.h>
#include <lanelet2_core/primitives/LineString.h>
#include <lanelet2_core/primitives/Point.h>
#include <lanelet2_core/primitives/Polygon.h>
#include <lanelet2_core/utility/Units.h>
#include "lanelet2_examples/internal/ExampleHelpers.h"
#undef NDEBUG
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wfloat-equal"
void part0Primitives();
void part1Points();
void part2LineStrings();
void part3Polygons();
void part4Lanelets();
void part5Areas();
void part6Geometry();
int main() {
part0Primitives();
part1Points();
part2LineStrings();
part3Polygons();
part4Lanelets();
part5Areas();
part6Geometry();
return 0;
}
void part0Primitives() {
using namespace lanelet;
using namespace lanelet::units::literals;
Point3d p(utils::getId(), 0, 0, 0);
Point3d pCopy = p;
assert(p == pCopy);
p.z() = 2;
p.setId(utils::getId());
assert(p.id() == pCopy.id());
assert(p.z() == pCopy.z());
p.attributes()["type"] = "point";
p.attributes()["pi"] = 3.14;
p.attributes()["velocity"] = "5 kmh";
assert(p.attributes() == pCopy.attributes());
assert(p.attribute("type") == "point");
assert(!!p.attribute("pi").asDouble());
assert(p.attribute("velocity").asVelocity() == 5_kmh);
assert(p.attributeOr("nonexistent", -1) == -1);
assert(p.attributeOr("type", 0) == 0);
assert(p.attributeOr("velocity", 0_kmh) == 5_kmh);
ConstPoint3d pConst = p;
assert(pConst.z() == 2);
p.z() = 3;
assert(pConst.z() == 3);
ConstPoint3d& pConstRef = p;
assert(pConstRef.z() == 3);
assert(p.constData() == pConst.constData());
}
void part1Points() {
using namespace lanelet;
Point3d p3d(utils::getId(), 1, 2, 3);
assert(p3d.x() == 1);
assert(p3d.y() == 2);
assert(p3d.z() == 3);
BasicPoint3d& p3dBasic = p3d.basicPoint();
p3dBasic.z() = 4;
assert(p3d.z() == 4);
BasicPoint3d pTwice = p3d.basicPoint() * 2;
assert(pTwice.z() == 8);
Point2d p2d = utils::to2D(p3d);
p2d.x() = 3;
assert(p3d.x() == 3);
assert(p3d.constData() == p2d.constData());
Point3d p3dNew = utils::to3D(p2d);
assert(p3dNew.z() == p3d.z());
assert(p3dNew.constData() == p3d.constData());
}
void part2LineStrings() {
using namespace lanelet;
Point3d p1{utils::getId(), 0, 0, 0};
Point3d p2{utils::getId(), 1, 0, 0};
Point3d p3{utils::getId(), 2, 0, 0};
LineString3d ls(utils::getId(), {p1, p2, p3});
assert(ls[1] == p2);
assert(ls.size() == 3);
for (Point3d& p : ls) {
assert(p.y() == 0);
}
ls.push_back(Point3d(utils::getId(), 3, 0, 0));
assert(ls.size() == 4);
ls.pop_back();
assert(ls.size() == 3);
ls.attributes()[AttributeName::Type] = AttributeValueString::LineThin;
ls.attributes()[AttributeName::Subtype] = AttributeValueString::Dashed;
assert(ls.numSegments() >= 2);
Segment3d segment = ls.segment(1);
assert(segment.first == p2);
assert(segment.second == p3);
LineString3d lsInv = ls.invert();
assert(lsInv.front() == p3);
assert(lsInv.inverted());
assert(lsInv.constData() == ls.constData());
assert(lsInv != ls);
LineString3d lsInvInv = lsInv.invert();
assert(lsInvInv == ls);
assert(lsInvInv.front() == p1);
ConstLineString3d lsConst = ls;
ConstPoint3d p1Const = ls[0];
assert(lsConst.constData() == ls.constData());
LineString2d ls2d = utils::to2D(ls);
Point2d front2d = ls2d.front();
assert(front2d == utils::to2D(p1));
ConstHybridLineString3d lsHybrid = utils::toHybrid(ls);
BasicPoint3d p1Basic = lsHybrid[0];
assert(p1Basic.x() == p1.x());
BasicLineString3d lsBasic = ls.basicLineString();
assert(lsBasic.front() == lsHybrid.front());
}
void part3Polygons() {
using namespace lanelet;
Point3d p1{utils::getId(), 0, 0, 0};
Point3d p2{utils::getId(), 1, 0, 0};
Point3d p3{utils::getId(), 2, -1, 0};
Polygon3d poly(utils::getId(), {p1, p2, p3});
assert(poly.size() == 3);
assert(poly.numSegments() == 3);
Segment3d lastSegment = poly.segment(2);
assert(lastSegment.first == p3);
assert(lastSegment.second == p1);
ConstPolygon3d polyConst = poly;
ConstHybridPolygon3d polyHybrid = utils::toHybrid(poly);
ConstPolygon2d poly2dConst = utils::to2D(polyConst);
assert(polyHybrid.constData() == poly2dConst.constData());
}
void part4Lanelets() {
using namespace lanelet;
LineString3d left = examples::getLineStringAtY(1);
LineString3d right = examples::getLineStringAtY(0);
Lanelet lanelet(utils::getId(), left, right);
assert(lanelet.leftBound() == left);
assert(lanelet.rightBound() == right);
lanelet.setLeftBound(left);
ConstLineString3d centerline = lanelet.centerline();
ConstLineString3d centerline2 = lanelet.centerline();
assert(centerline2 == centerline);
lanelet.setLeftBound(examples::getLineStringAtY(2));
ConstLineString3d centerline3 = lanelet.centerline();
assert(centerline3 != centerline);
right.push_back(Point3d(utils::getId(), 4, 0, 0));
assert(centerline3 == lanelet.centerline());
lanelet.resetCache();
assert(centerline3 != lanelet.centerline());
right.pop_back();
lanelet.resetCache();
Lanelet laneletInv = lanelet.invert();
assert(laneletInv.leftBound().front() == lanelet.rightBound().back());
assert(laneletInv.constData() == lanelet.constData());
CompoundPolygon3d polygon = lanelet.polygon3d();
assert(polygon.size() == 6);
assert(polygon[0] == lanelet.leftBound().front());
assert(polygon.back() == lanelet.rightBound().front());
ConstLanelet laneletConst = lanelet;
assert(laneletConst.constData() == lanelet.constData());
ConstLineString3d leftConst = lanelet.leftBound();
assert(lanelet.regulatoryElements().empty());
}
void part5Areas() {
using namespace lanelet;
LineString3d top = examples::getLineStringAtY(2);
LineString3d right = examples::getLineStringAtX(2).invert();
LineString3d bottom = examples::getLineStringAtY(0).invert();
LineString3d left = examples::getLineStringAtY(0);
Area area(utils::getId(), {top, right, bottom, left});
LineStrings3d outer = area.outerBound();
area.setOuterBound(outer);
CompoundPolygon3d outerPolygon = area.outerBoundPolygon();
assert(area.innerBounds().empty());
ConstArea areaConst = area;
ConstLineStrings3d outerConst = areaConst.outerBound();
assert(area.regulatoryElements().empty());
}
void part6Geometry() {
using namespace lanelet;
Point3d point(utils::getId(), 1, 4, 1);
LineString3d ls = examples::getLineStringAtY(2);
Polygon3d poly = examples::getAPolygon();
Lanelet lanelet = examples::getALanelet();
Area area = examples::getAnArea();
ConstHybridLineString3d lsHybrid = utils::toHybrid(ls);
ConstHybridPolygon3d polyHybrid = utils::toHybrid(poly);
auto dP2Line3d = geometry::distance(point, lsHybrid);
assert(dP2Line3d > 2);
auto dP2Line2d = geometry::distance(utils::to2D(point), utils::to2D(lsHybrid));
assert(dP2Line2d == 2);
auto l3d = geometry::length(lsHybrid);
assert(l3d == 2);
auto ar = geometry::area(utils::to2D(polyHybrid));
assert(ar == 2);
BasicPoint3d pProj = geometry::project(ls, point);
assert(pProj.y() == 2);
ArcCoordinates arcCoordinates =
geometry::toArcCoordinates(utils::to2D(ls), utils::to2D(point));
assert(arcCoordinates.distance == 2);
assert(arcCoordinates.length == 1);
BoundingBox3d pointBox = geometry::boundingBox3d(point);
BoundingBox3d lsBox = geometry::boundingBox3d(ls);
BoundingBox2d laneletBox = geometry::boundingBox2d(lanelet);
BoundingBox2d areaBox = geometry::boundingBox2d(area);
assert(geometry::intersects(laneletBox, areaBox));
assert(!geometry::intersects(pointBox, lsBox));
assert(geometry::overlaps3d(area, lanelet, 3));
}
结语
如果您有修改意见或问题,欢迎留言或者通过邮箱和我联系。 手打很辛苦,如果我的文章对您有帮助,转载请注明出处。
|