From 8bc1fcc88fe35a1f0308b32c6f53d84b22bf024f Mon Sep 17 00:00:00 2001 From: Dtouch <1472779700@qq.com> Date: Mon, 4 Nov 2024 01:04:08 +0800 Subject: [PATCH] interface --- .gitignore | 9 +++ CMakeLists.txt | 8 +++ include/line.hpp | 108 ++++++++++++++++++++++++++++++++++ include/real.hpp | 3 + include/solid.hpp | 16 ++++++ include/vec.hpp | 144 ++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 26 +++++++++ 7 files changed, 314 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 include/line.hpp create mode 100644 include/real.hpp create mode 100644 include/solid.hpp create mode 100644 include/vec.hpp create mode 100644 main.cpp diff --git a/.gitignore b/.gitignore index e257658..a15b157 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,12 @@ *.out *.app +.idea/ +.vscode/ + + *build*/ + **/*build*/ +cmake-build-debug/ + + + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..593ad67 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.30) +project(PMClassifier) + +set(CMAKE_CXX_STANDARD 20) + +include_directories(include) + +add_executable(PMClassifier main.cpp) diff --git a/include/line.hpp b/include/line.hpp new file mode 100644 index 0000000..92093d1 --- /dev/null +++ b/include/line.hpp @@ -0,0 +1,108 @@ +#pragma once +#include "vec.hpp" +#include +#include +#include + +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 getClosestParam(const Vec3& p) const = 0; +}; + +template +using PtArray = std::vector >; +using Pt3Array = PtArray<3>; +using Pt2Array = PtArray<2>; + +template +class Polyline: public ILine { +public: + Polyline(const PtArray &points, const std::vector &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; + +private: + PtArray points; + std::vector bugles; + bool closed; + +public: + + Vec3 eval(const ILineParam& param) const override { + const PolylineParam* polyParam = dynamic_cast(¶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 getClosestParam(const Vec3& p) const override { + auto closestParam = std::make_unique(); + // 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: +}; \ No newline at end of file diff --git a/include/real.hpp b/include/real.hpp new file mode 100644 index 0000000..ba82272 --- /dev/null +++ b/include/real.hpp @@ -0,0 +1,3 @@ +#pragma once + +using real = double; \ No newline at end of file diff --git a/include/solid.hpp b/include/solid.hpp new file mode 100644 index 0000000..5899adc --- /dev/null +++ b/include/solid.hpp @@ -0,0 +1,16 @@ +#pragma once +#include +#include +#include + +class ISolid { +public: + virtual ~ISolid() = default; + + virtual real sdf(Vec3 p); +}; + +class IExtrudedSolid : public ISolid { +protected: + virtual Vec3 getClosestPointOnAxis(const Vec3 &p); +}; diff --git a/include/vec.hpp b/include/vec.hpp new file mode 100644 index 0000000..9f58cdb --- /dev/null +++ b/include/vec.hpp @@ -0,0 +1,144 @@ +#pragma once +#include "real.hpp" +#include +#include + +template +class Vec { +public: + using real = float; // 根据需要定义 real 类型 + std::array data; + + Vec() { + data.fill(0); + } + + Vec(std::initializer_list values) { + std::copy(values.begin(), values.end(), data.begin()); + } + + Vec(const Vec& v) { + data = v.data; + } + + Vec& operator=(const Vec& v) { + data = v.data; + return *this; + } + + real& operator[](size_t index) { + return data[index]; + } + + const real& operator[](size_t index) const { + return data[index]; + } + + Vec operator+(const Vec& v) const { + Vec result; + for (size_t i = 0; i < N; ++i) { + result[i] = data[i] + v[i]; + } + return result; + } + + Vec operator-(const Vec& v) const { + Vec result; + for (size_t i = 0; i < N; ++i) { + result[i] = data[i] - v[i]; + } + return result; + } + + Vec operator*(real s) const { + Vec result; + for (size_t i = 0; i < N; ++i) { + result[i] = data[i] * s; + } + return result; + } + + Vec operator/(real s) const { + Vec result; + for (size_t i = 0; i < N; ++i) { + result[i] = data[i] / s; + } + return result; + } + + real dot(const Vec& v) const { + real sum = 0; + for (size_t i = 0; i < N; ++i) { + sum += data[i] * v[i]; + } + return sum; + } + + real norm() const { + return std::sqrt(dot(*this)); + } + + Vec normalize() const { + return *this / norm(); + } + + Vec reflect(const Vec& n) const { + return *this - n * 2 * dot(n); + } +}; + +template + +using Vec2 = Vec<2>; +using Vec3 = Vec<3>; + + +//class Vec3 { +//public: +// real x, y, z; +// Vec3(): x(0), y(0), z(0) {} +// Vec3(real x, real y, real z): x(x), y(y), z(z) {} +// Vec3(const Vec3& v): x(v.x), y(v.y), z(v.z) {} +// Vec3& operator=(const Vec3& v) { +// x = v.x; +// y = v.y; +// z = v.z; +// return *this; +// } +// Vec3 operator+(const Vec3& v) const { +// return Vec3(x + v.x, y + v.y, z + v.z); +// } +// Vec3 operator-(const Vec3& v) const { +// return Vec3(x - v.x, y - v.y, z - v.z); +// } +// Vec3 operator*(real s) const { +// return Vec3(x * s, y * s, z * s); +// } +// Vec3 operator*(const Vec3& v) const { +// return Vec3(x * v.x, y * v.y, z * v.z); +// } +// Vec3 operator-() const { +// return Vec3(-x, -y, -z); +// } +// friend Vec3 operator*(real s, const Vec3& v) { +// return Vec3(s * v.x, s * v.y, s * v.z); +// } +// Vec3 operator/(real s) const { +// return Vec3(x / s, y / s, z / s); +// } +// real dot(const Vec3& v) const { +// return x * v.x + y * v.y + z * v.z; +// } +// Vec3 cross(const Vec3& v) const { +// return Vec3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); +// } +// real norm() const { +// return sqrt(x * x + y * y + z * z); +// } +// Vec3 normalize() const { +// return *this / norm(); +// } +// Vec3 reflect(const Vec3& n) const { +// return *this - n * 2 * dot(n); +// } +//}; \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..55e70af --- /dev/null +++ b/main.cpp @@ -0,0 +1,26 @@ +#include +#include + +// TIP To Run code, press or +// click the icon in the gutter. +int main() { + // TIP Press when your caret is at the + // lang variable name to see how CLion can help you rename it. + auto lang = "C++"; + std::cout << "Hello and welcome to " << lang << "!\n"; + + for (int i = 1; i <= 5; i++) { + // TIP Press to start debugging your code. + // We have set one + // breakpoint for you, but you can always add more by pressing + // . + std::cout << "i = " << i << std::endl; + } + + return 0; +} + +// TIP See CLion help at jetbrains.com/help/clion/. +// Also, you can try interactive lessons for CLion by selecting +// 'Help | Learn IDE Features' from the main menu.