You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.4 KiB
108 lines
2.4 KiB
#pragma once
|
|
#include "vec.hpp"
|
|
#include <vector>
|
|
#include <iostream>
|
|
#include <memory>
|
|
|
|
class ILineParam {
|
|
public:
|
|
virtual ~ILineParam() = default;
|
|
};
|
|
|
|
struct PolylineParam : ILineParam {
|
|
int segIdx;
|
|
real tOnSeg;
|
|
};
|
|
|
|
struct PolynomialLineParam : ILineParam {
|
|
real t;
|
|
};
|
|
|
|
class ILine {
|
|
public:
|
|
virtual ~ILine() = default;
|
|
|
|
virtual Vec3 eval(const ILineParam& param) const = 0;
|
|
virtual Vec3 tangent(const ILineParam& param) const = 0;
|
|
virtual std::unique_ptr<ILineParam> getClosestParam(const Vec3& p) const = 0;
|
|
};
|
|
|
|
template<size_t N>
|
|
using PtArray = std::vector<Vec<N> >;
|
|
using Pt3Array = PtArray<3>;
|
|
using Pt2Array = PtArray<2>;
|
|
|
|
template<size_t N>
|
|
class Polyline: public ILine {
|
|
public:
|
|
Polyline(const PtArray<N> &points, const std::vector<double> &bugles, bool closed = false) : points(points),
|
|
bugles(bugles), closed(closed) {
|
|
assert(points.size() >= 2);
|
|
if (closed) {
|
|
assert(points.size() == bugles.size());
|
|
} else {
|
|
assert(points.size() - 1 == bugles.size() || points.size() == bugles.size());
|
|
}
|
|
}
|
|
|
|
using Point = Vec<N>;
|
|
|
|
private:
|
|
PtArray<N> points;
|
|
std::vector<double> bugles;
|
|
bool closed;
|
|
|
|
public:
|
|
|
|
Vec3 eval(const ILineParam& param) const override {
|
|
const PolylineParam* polyParam = dynamic_cast<const PolylineParam*>(¶m); // 进行类型检查
|
|
if (!polyParam) {
|
|
throw std::invalid_argument("Invalid parameter type for Polyline::eval");
|
|
}
|
|
// TODO:
|
|
}
|
|
|
|
Vec3 tangent(const ILineParam& param) const override {
|
|
// TODO:
|
|
}
|
|
|
|
std::unique_ptr<ILineParam> getClosestParam(const Vec3& p) const override {
|
|
auto closestParam = std::make_unique<PolylineParam>();
|
|
// TODO:
|
|
return closestParam;
|
|
}
|
|
|
|
void addPoint(const Point &point) {
|
|
points.push_back(point);
|
|
}
|
|
|
|
|
|
|
|
const Point &getPoint(size_t index) const {
|
|
return points[index];
|
|
}
|
|
|
|
size_t size() const {
|
|
return points.size();
|
|
}
|
|
|
|
void clear() {
|
|
points.clear();
|
|
}
|
|
|
|
void print() const {
|
|
for (const auto &point: points) {
|
|
std::cout << "(";
|
|
for (size_t i = 0; i < N; ++i) {
|
|
std::cout << point[i];
|
|
if (i < N - 1) std::cout << ", ";
|
|
}
|
|
std::cout << ") ";
|
|
}
|
|
std::cout << std::endl;
|
|
}
|
|
};
|
|
|
|
class PolynomialLine: public ILine {
|
|
public:
|
|
};
|