Browse Source

interface

master
Dtouch 4 months ago
parent
commit
8bc1fcc88f
  1. 9
      .gitignore
  2. 8
      CMakeLists.txt
  3. 108
      include/line.hpp
  4. 3
      include/real.hpp
  5. 16
      include/solid.hpp
  6. 144
      include/vec.hpp
  7. 26
      main.cpp

9
.gitignore

@ -32,3 +32,12 @@
*.out
*.app
.idea/
.vscode/
*build*/
**/*build*/
cmake-build-debug/

8
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)

108
include/line.hpp

@ -0,0 +1,108 @@
#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*>(&param); // 进行类型检查
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:
};

3
include/real.hpp

@ -0,0 +1,3 @@
#pragma once
using real = double;

16
include/solid.hpp

@ -0,0 +1,16 @@
#pragma once
#include <real.hpp>
#include <vec.hpp>
#include <line.hpp>
class ISolid {
public:
virtual ~ISolid() = default;
virtual real sdf(Vec3 p);
};
class IExtrudedSolid : public ISolid {
protected:
virtual Vec3 getClosestPointOnAxis(const Vec3 &p);
};

144
include/vec.hpp

@ -0,0 +1,144 @@
#pragma once
#include "real.hpp"
#include <cmath>
#include <array>
template <size_t N>
class Vec {
public:
using real = float; // 根据需要定义 real 类型
std::array<real, N> data;
Vec() {
data.fill(0);
}
Vec(std::initializer_list<real> values) {
std::copy(values.begin(), values.end(), data.begin());
}
Vec(const Vec<N>& v) {
data = v.data;
}
Vec<N>& operator=(const Vec<N>& 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<N> operator+(const Vec<N>& v) const {
Vec<N> result;
for (size_t i = 0; i < N; ++i) {
result[i] = data[i] + v[i];
}
return result;
}
Vec<N> operator-(const Vec<N>& v) const {
Vec<N> result;
for (size_t i = 0; i < N; ++i) {
result[i] = data[i] - v[i];
}
return result;
}
Vec<N> operator*(real s) const {
Vec<N> result;
for (size_t i = 0; i < N; ++i) {
result[i] = data[i] * s;
}
return result;
}
Vec<N> operator/(real s) const {
Vec<N> result;
for (size_t i = 0; i < N; ++i) {
result[i] = data[i] / s;
}
return result;
}
real dot(const Vec<N>& 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<N> normalize() const {
return *this / norm();
}
Vec<N> reflect(const Vec<N>& n) const {
return *this - n * 2 * dot(n);
}
};
template <size_t N>
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);
// }
//};

26
main.cpp

@ -0,0 +1,26 @@
#include <iostream>
#include <solid.hpp>
// TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
int main() {
// TIP Press <shortcut actionId="RenameElement"/> when your caret is at the
// <b>lang</b> 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 <shortcut actionId="Debug"/> to start debugging your code.
// We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/>
// breakpoint for you, but you can always add more by pressing
// <shortcut actionId="ToggleLineBreakpoint"/>.
std::cout << "i = " << i << std::endl;
}
return 0;
}
// TIP See CLion help at <a
// href="https://www.jetbrains.com/help/clion/">jetbrains.com/help/clion/</a>.
// Also, you can try interactive lessons for CLion by selecting
// 'Help | Learn IDE Features' from the main menu.
Loading…
Cancel
Save