Skip to content

Commit

Permalink
补充注释。
Browse files Browse the repository at this point in the history
  • Loading branch information
vistart committed Aug 20, 2023
1 parent 5b4f3b6 commit 6b10943
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 61 deletions.
15 changes: 8 additions & 7 deletions src/camotics/sim/MoveLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@


namespace CAMotics {
class MoveLookup {
class MoveLookup { // 表示一个移动查找器。这个类用来存储和查询GCode::Move对象,表示G代码中的移动指令。这个类有以下特点:
public:
virtual ~MoveLookup() {}
virtual ~MoveLookup() { // 虚析构函数,用来释放内存。

virtual cb::Rectangle3D getBounds() const = 0;
virtual void insert(const GCode::Move *move,
virtual cb::Rectangle3D getBounds() const = 0; // 纯虚函数getBounds,返回移动查找器的边界矩形。
virtual void insert(const GCode::Move *move, // 纯虚函数insert,接受一个GCode::Move对象的指针和一个边界矩形作为参数,将它们插入到移动查找器中。
const cb::Rectangle3D &bbox) = 0;
virtual bool intersects(const cb::Rectangle3D &r) const = 0;
virtual void collisions(const cb::Vector3D &p,
virtual bool intersects(const cb::Rectangle3D &r) const = 0; // 纯虚函数intersects,接受一个矩形作为参数,判断它是否与移动查找器相交。
virtual void collisions(const cb::Vector3D &p, // 纯虚函数collisions,接受一个三维向量和一个GCode::Move对象指针的向量作为参数。这个函数用来查找移动查找器中与参数向量相交的所有GCode::Move对象,并将它们的指针存储到参数向量中。

std::vector<const GCode::Move *> &moves) const = 0;
virtual void finalize() {}
virtual void finalize() {} // 虚函数finalize,用来对移动查找器进行优化或清理。这个函数默认为空。
};
}
16 changes: 8 additions & 8 deletions src/camotics/sim/ReduceTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ using namespace std;
using namespace cb;
using namespace CAMotics;


ReduceTask::ReduceTask(const SmartPointer<Surface> &surface) :
// 实现了ReduceTask类的成员函数。这个类表示一个简化表面的任务,用来对表面进行简化,减少顶点和三角形的数量,提高渲染效率。这个类继承了Task类,表示一个异步的任务,用来执行模拟的计算。这个类的成员函数有以下功能:
ReduceTask::ReduceTask(const SmartPointer<Surface> &surface) : // 构造函数:接受一个Surface对象的智能指针作为参数,用来初始化surface。Surface对象表示一个三维的表面,由一组顶点和三角形组成。
surface(surface) {}


void ReduceTask::run() {
LOG_INFO(1, "Reducing mesh");
void ReduceTask::run() { // run函数:重写了父类Task的虚函数。这个函数用来对surface进行简化,并记录简化的时间和效果。这个函数做了以下步骤:
LOG_INFO(1, "Reducing mesh"); // 打印一条日志信息,表示开始简化网格。

double startTime = Timer::now();
double startCount = surface->getTriangleCount();
double startTime = Timer::now(); // 获取当前的时间和表面的三角形数量,作为简化前的数据。
double startCount = surface->getTriangleCount(); // 调用surface的reduce方法进行简化,传入自身作为参数。这个方法会根据一些算法和条件,删除一些不必要或冗余的顶点和三角形,同时保持表面的形状和质量。

surface->reduce(*this);

unsigned count = surface->getTriangleCount();
unsigned count = surface->getTriangleCount(); // 获取简化后的表面的三角形数量,并计算出简化的百分比。
double r = (double)(startCount - count) / startCount * 100;

LOG_INFO(1, "Time: " << TimeInterval(Timer::now() - startTime)
LOG_INFO(1, "Time: " << TimeInterval(Timer::now() - startTime // 打印一条日志信息,表示结束简化网格,并显示简化所花费的时间,以及表面的三角形数量和简化百分比。
<< String::printf(" Triangles: %u Reduction: %0.2f%%", count, r));
}
10 changes: 5 additions & 5 deletions src/camotics/sim/ReduceTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
namespace CAMotics {
class Surface;

class ReduceTask : public Task {
cb::SmartPointer<Surface> surface;
class ReduceTask : public Task { // 表示一个简化表面的任务。这个类继承了Task类,表示一个异步的任务,用来执行模拟的计算。这个类有以下特点:
cb::SmartPointer<Surface> surface; // surface成员变量,是一个Surface对象的智能指针。Surface对象表示一个三维的表面,由一组顶点和三角形组成。

public:
ReduceTask(const cb::SmartPointer<Surface> &surface);
ReduceTask(const cb::SmartPointer<Surface> &surface); // 构造函数,接受一个Surface对象的智能指针作为参数,用来初始化surface。

const cb::SmartPointer<Surface> &getSurface() const {return surface;}
const cb::SmartPointer<Surface> &getSurface() const {return surface; // getSurface方法,返回surface的常量引用。

// From Task
void run();
void run(); // run方法,重写了父类Task的虚函数。这个方法用来对surface进行简化,减少顶点和三角形的数量,提高渲染效率。这个方法会调用surface的reduce方法进行简化。
};
}
13 changes: 7 additions & 6 deletions src/camotics/sim/Simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ using namespace std;
using namespace cb;
using namespace CAMotics;

// 实现了Simulation类的成员函数。这个类表示一个切割模拟的参数和结果,用来序列化和反序列化为JSON格式。这个类继承了cb::JSON::Serializable类,表示一个可以序列化和反序列化为JSON格式的对象。这个类的成员函数有以下功能:
Simulation::~Simulation() {} // 析构函数:释放内存。

Simulation::~Simulation() {}


string Simulation::computeHash() const {
string Simulation::computeHash() const { // computeHash函数:返回模拟的哈希值。哈希值是一种用来标识和比较数据的字符串,通常由一些数字和字母组成。这个函数使用了SHA256算法和Base64编码来生成哈希值。
SHA256 sha256;
UpdateStreamFilter<SHA256> digest(sha256);

Expand All @@ -58,7 +58,7 @@ string Simulation::computeHash() const {
}


void Simulation::read(const JSON::Value &value) {
void Simulation::read(const JSON::Value &value) { // read函数:重写了父类cb::JSON::Serializable的虚函数,接受一个JSON::Value对象作为参数,表示一个JSON格式的数据。这个函数用来将JSON格式的数据反序列化为模拟对象,并初始化其成员变量。这个函数会根据JSON数据中的键值对,创建并读取相应的对象,如工具表、工件、工具路径、表面和规划器配置。
resolution = value.getNumber("resolution", 0);
time = value.getNumber("time", 0);
mode = RenderMode::parse(value.getString("render-mode", mode.toString()));
Expand Down Expand Up @@ -87,8 +87,9 @@ void Simulation::read(const JSON::Value &value) {
}


void Simulation::write(JSON::Sink &sink) const {
sink.beginDict();
void Simulation::write(JSON::Sink &sink) const { // write函数:重写了父类cb::JSON::Serializable的虚函数,接受一个JSON::Sink对象作为参数,表示一个JSON格式的输出流。这个函数用来将模拟对象序列化为JSON格式的数据,并写入到输出流中。这个函数会根据模拟对象的成员变量,创建并写入相应的键值对,如分辨率、时间、渲染模式、工具表、工件、工具路径、表面和规划器配置。

sink.beginDict();

sink.insert("resolution", resolution);
sink.insert("time", time);
Expand Down
29 changes: 16 additions & 13 deletions src/camotics/sim/Simulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,34 @@ namespace cb {namespace JSON {class Sink;}}
namespace CAMotics {
class Workpiece;

class Simulation : public cb::JSON::Serializable {
class Simulation : public cb::JSON::Serializable { // 表示一个切割模拟的参数和结果。这个类继承了cb::JSON::Serializable类,表示一个可以序列化和反序列化为JSON格式的对象。这个类有以下特点:

public:
cb::SmartPointer<GCode::ToolPath> path;
cb::SmartPointer<GCode::PlannerConfig> planConf;
cb::SmartPointer<Surface> surface;
Workpiece workpiece;
double resolution;
double time;
RenderMode mode;
unsigned threads;
cb::SmartPointer<GCode::ToolPath> path; // path成员变量,是一个GCode::ToolPath对象的智能指针。GCode::ToolPath对象表示一个G代码的工具路径,包含了一系列的移动指令和工具信息。
cb::SmartPointer<GCode::PlannerConfig> planConf; // planConf成员变量,是一个GCode::PlannerConfig对象的智能指针。GCode::PlannerConfig对象表示一个G代码的规划器配置,包含了一些控制移动速度和加速度的参数。
cb::SmartPointer<Surface> surface; // surface成员变量,是一个Surface对象的智能指针。Surface对象表示一个三维的表面,由一组顶点和三角形组成。

Workpiece workpiece; // workpiece成员变量,是一个Workpiece对象。Workpiece对象表示一个原始的工件,是一个三维矩形。
double resolution; // resolution成员变量,是一个双精度浮点数。它表示模拟的分辨率,单位是米。
double time; // time成员变量,是一个双精度浮点数。它表示模拟的时间,单位是秒。
RenderMode mode; // mode成员变量,是一个RenderMode枚举类型。它表示模拟的渲染模式,可以是实体模式、线框模式或者混合模式。
unsigned threads; // threads成员变量,是一个无符号整数。它表示模拟使用的线程数量。

Simulation(const cb::SmartPointer<GCode::ToolPath> &path,
const cb::SmartPointer<GCode::PlannerConfig> &planConf,
const cb::SmartPointer<Surface> &surface,
const Workpiece &workpiece, double resolution, double time,
RenderMode mode, unsigned threads) :
RenderMode mode, unsigned threads) : // 构造函数,接受以上所有成员变量作为参数,用来初始化它们。
path(path), planConf(planConf), surface(surface), workpiece(workpiece),
resolution(resolution), time(time), mode(mode), threads(threads) {}
~Simulation();
~Simulation(); // 析构函数,释放内存。

const GCode::ToolTable &getTools() const {return path->getTools();}
const GCode::ToolTable &getTools() const {return path->getTools();} // getTools方法,返回path中的工具表的常量引用。工具表是一个存储了工具编号和工具形状的映射表。

std::string computeHash() const;
std::string computeHash() const; // computeHash方法,返回模拟的哈希值。哈希值是一种用来标识和比较数据的字符串,通常由一些数字和字母组成。

// From JSON::Serializable
// read和write方法,重写了父类cb::JSON::Serializable的虚函数。这两个方法用来将模拟对象序列化和反序列化为JSON格式。JSON格式是一种轻量级的数据交换格式,通常由一些键值对组成。
using cb::JSON::Serializable::read;
using cb::JSON::Serializable::write;
void read(const cb::JSON::Value &value);
Expand Down
22 changes: 11 additions & 11 deletions src/camotics/sim/SimulationRun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ SmartPointer<MoveLookup> SimulationRun::getMoveLookup() const {
void SimulationRun::setEndTime(double endTime) {sim.time = endTime;}


SmartPointer<Surface> SimulationRun::compute(Task &task) {
Rectangle3D bbox;
SmartPointer<Surface> SimulationRun::compute(Task &task) { // 这个方法接受一个Task对象作为参数,表示一个异步的任务,用来执行模拟的计算。这个方法的具体流程如下:
Rectangle3D bbox; // 首先,声明一个矩形变量bbox,用来存储模拟的边界。

double start = Timer::now();
double start = Timer::now(); // 然后,获取当前的时间和模拟的时间,并取其中较小的一个作为模拟的结束时间。打印一条日志信息,表示开始计算表面。
double simTime = std::min(sim.path->getTime(), sim.time);

LOG_INFO(1, "Computing surface at " << TimeInterval(simTime));

// Build full sweep once OR for each file
if (sweep.isNull()) {
if (sweep.isNull()) { // 接着,判断sweep是否为空。如果为空,则说明是第一次进行模拟,需要创建一个ToolSweep对象,并将其赋值给sweep。ToolSweep对象表示一个工具扫过的形状,用来模拟切割过程。这个对象根据sim中的工具路径创建,并覆盖整个时间段。然后,根据sim中的工件获取其边界,并将其扩大一点作为bbox。接着,创建一个GridTree对象,并将其赋值给tree。GridTree对象表示一个网格树,用来存储和查询表面的数据。这个对象根据bbox和sim中的分辨率创建一个网格。
// GCode::Tool sweep
sweep = new ToolSweep(sim.path); // Build sweep for entire time period

Expand All @@ -69,7 +69,7 @@ SmartPointer<Surface> SimulationRun::compute(Task &task) {
// Grid
tree = new GridTree(Grid(bbox, sim.resolution));

} else {
} else { // 如果sweep不为空,则说明是继续进行模拟,需要更新sweep中的移动查找器。移动查找器是一个存储和查询GCode::Move对象的结构,表示G代码中的移动指令。首先计算出最小和最大的时间,分别表示模拟的起始和结束时间。然后创建一个ToolSweep对象,并将其赋值给change。这个对象根据sim中的工具路径和最小和最大时间创建,并只覆盖这个时间段。然后调用sweep的setChange方法,将change设置为sweep中的移动查找器。接着,根据change获取其边界,并将其扩大一点作为bbox。
double minTime = simTime;
double maxTime = simTime;

Expand All @@ -82,24 +82,24 @@ SmartPointer<Surface> SimulationRun::compute(Task &task) {
}

// Set target time
sweep->setEndTime(simTime);
sweep->setEndTime(simTime); // 然后,调用sweep的setEndTime方法,将模拟的结束时间设置为simTime。

// Setup cut simulation
CutWorkpiece cutWP(sweep, sim.workpiece);
CutWorkpiece cutWP(sweep, sim.workpiece); // 接着,创建一个CutWorkpiece对象,并将其赋值给cutWP。CutWorkpiece对象表示一个被切割的工件,用来计算空间中的点到工件表面的距离。这个对象根据sweep和sim中的工件创建。

// Render
Renderer renderer(task);
Renderer renderer(task); // 然后,创建一个Renderer对象,并将其赋值给renderer。Renderer对象用来渲染cutWP到tree中,并传入task作为参数。Task对象表示一个异步的任务,用来执行模拟的计算。
renderer.render(cutWP, *tree, bbox, sim.threads, sim.mode);

if (task.shouldQuit()) {
if (task.shouldQuit()) { // 接着,判断task是否应该退出。如果是,则释放sweep和tree,并返回空指针。
sweep.release();
tree.release();
return 0;
}

LOG_DEBUG(1, "Render time " << TimeInterval(Timer::now() - start));
LOG_DEBUG(1, "Render time " << TimeInterval(Timer::now() - start)); // 如果不是,则打印一条日志信息,表示渲染所花费的时间。

// Extract surface
lastTime = simTime;
lastTime = simTime; // 最后,更新lastTime为simTime,并返回一个TriangleSurface对象的智能指针。TriangleSurface对象表示一个三维的表面,由一组顶点和三角形组成。这个对象根据tree创建。
return new TriangleSurface(*tree);
}
22 changes: 11 additions & 11 deletions src/camotics/sim/SimulationRun.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ namespace CAMotics {
class Task;


class SimulationRun {
Simulation sim;
cb::SmartPointer<ToolSweep> sweep;
cb::SmartPointer<GridTree> tree;
class SimulationRun { // 表示一个切割模拟的运行过程。这个类用来根据一个Simulation对象的参数,计算出一个Surface对象的结果。这个类有以下特点:
Simulation sim; // sim成员变量,是一个Simulation对象。Simulation对象表示一个切割模拟的参数和结果,用来序列化和反序列化为JSON格式。
cb::SmartPointer<ToolSweep> sweep; // sweep成员变量,是一个ToolSweep对象的智能指针。ToolSweep对象表示一个工具扫过的形状,用来模拟切割过程。
cb::SmartPointer<GridTree> tree; // tree成员变量,是一个GridTree对象的智能指针。GridTree对象表示一个网格树,用来存储和查询表面的数据。

double lastTime = 0;
double lastTime = 0; // lastTime成员变量,是一个双精度浮点数。它表示模拟的最后一次更新的时间,单位是秒。

public:
SimulationRun(const Simulation &sim);
~SimulationRun();
SimulationRun(const Simulation &sim); // 构造函数,接受一个Simulation对象作为参数,用来初始化sim,并根据sim中的工具路径和工具形状创建sweep。
~SimulationRun(); // 析构函数,释放内存。

Simulation &getSimulation() {return sim;}
Simulation &getSimulation() {return sim;} // getSimulation方法,返回sim的引用。

cb::SmartPointer<MoveLookup> getMoveLookup() const;
cb::SmartPointer<MoveLookup> getMoveLookup() const; // getMoveLookup方法,返回sweep中的移动查找器的智能指针。移动查找器是一个存储和查询GCode::Move对象的结构,表示G代码中的移动指令。

void setEndTime(double endTime);
void setEndTime(double endTime); // setEndTime方法,接受一个双精度浮点数作为参数,表示模拟的结束时间。这个方法用来设置sim中的时间,并根据时间调整sweep中的移动查找器。

cb::SmartPointer<Surface> compute(Task &task);
cb::SmartPointer<Surface> compute(Task &task); // compute方法,接受一个Task对象作为参数。这个方法用来根据sweep和workpiece计算出表面,并返回一个Surface对象的智能指针。这个方法会创建并更新tree,并调用其compute方法进行计算,并传入task作为参数。Task对象表示一个异步的任务,用来执行模拟的计算。
};
}

0 comments on commit 6b10943

Please sign in to comment.