File size: 6,833 Bytes
1dd0e3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b91790
1dd0e3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#pragma once

#include <cstdint>
#include <glad/glad.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <vector>
#include <string>
#include <chrono>
#include <thread>
#include <atomic>
#include <mutex>
#include "object_pool.h"
#include "stl_parser.h"
#include "bvh.h"
#include "geometry_api.h"
#include "llm_client.h"
#include "tool_registry.h"
#include "embodied_ai_agent.h"
#include "highlight_types.h"
#include "command_dispatcher.h"
//#include "imgui/imgui.h"
#include "../Algorithm/GeometryExpert.h"

namespace hhb {
namespace render {

using HighlightType = hhb::core::HighlightType;

class RenderManager {
public:
    RenderManager(int width, int height, const std::string& title);
    ~RenderManager();

    bool initialize();
    void render();
    void processInput();
    bool shouldClose();
    void swapBuffers();
    void setTriangles(hhb::core::ObjectPool<hhb::core::Triangle>& pool);
    void loadFile(const std::string& filename);
    void centerModel();
    void resetCameraToModel() { centerModel(); }
    
    void setAutoRotate(bool enabled) { autoRotate = enabled; }
    bool getAutoRotate() const { return autoRotate; }

    void setAutomationMode(bool enabled) { automationMode = enabled; }
    bool getAutomationMode() const { return automationMode; }
    
    void setZoom(float value) { zoom = value; }
    float getZoom() const { return zoom; }
    
    float* getCameraRotation() { return cameraRotation; }
    float* getCameraPosition() { return cameraPosition; }

    void setTargetCameraPosition(const float pos[3], float duration = 0.5f);
    void setTargetCameraRotation(const float rot[2], float duration = 0.5f);
    void setTargetZoom(float target, float duration = 0.5f);
    bool isCameraAnimating() const;
    void stopCameraAnimation();

    void initSkills();

    const std::vector<hhb::core::Triangle*>& getTrianglePtrs() const { return trianglePtrs; }

    struct SpatialInfo {
        float center[3];
        float bounds[6];
        float cameraPos[3];
        float cameraRot[2];
        float currentZoom;
    };
    
    SpatialInfo getSpatialInfo();

    // 具身智能 Agent 的结果回调:当工具执行完成后更新高亮
    void onToolResult(const std::vector<int>& indices, HighlightType type, const std::string& desc);

    struct CaptureConfig {
        int sampleCount;
        std::string outputDir;
        float cameraRadius;
        int imageWidth;
        int imageHeight;
        bool saveDepth;
        bool saveMask;
        CaptureConfig()
            : sampleCount(1000), outputDir("synthetic_output"),
              cameraRadius(5.0f), imageWidth(800), imageHeight(600),
              saveDepth(false), saveMask(false) {}
    };

    struct CaptureResult {
        int totalFrames;
        int successFrames;
        int failedFrames;
        float elapsedSeconds;
        std::string outputDirectory;
    };

    CaptureResult captureSyntheticData(const CaptureConfig& config);

private:
    GLFWwindow* window;
    int width;
    int height;
    std::string title;

    GLuint VAO, VBO;
    GLuint shaderProgram;

    GLuint labelVAO, labelVBO;
    GLuint labelShaderProgram;
    bool labelModeActive;
    std::vector<int> faceCategoryIds;
    bool faceCategoriesComputed;

    std::vector<float> vertexData;
    size_t triangleCount;
    hhb::core::ObjectPool<hhb::core::Triangle>* trianglePool;
    std::vector<hhb::core::Triangle*> trianglePtrs;
    hhb::core::BVH bvh;

    float cameraPosition[3];
    float cameraRotation[2];
    float zoom;
    
    bool autoRotate;
    float autoRotateSpeed;
    bool automationMode;

    bool cameraAnimating;
    float targetCameraPos[3];
    float targetCameraRot[2];
    float targetZoom;
    float cameraPosStart[3];
    float cameraRotStart[2];
    float zoomStart;
    std::chrono::steady_clock::time_point cameraAnimStart;
    float cameraAnimDuration;

    std::chrono::steady_clock::time_point lastFrame;
    float deltaTime;
    int frameCount;
    float lastFpsUpdate;
    float fps;

    float metallic;
    float roughness;

    float loadTime;
    size_t memoryUsage;

    hhb::core::Triangle* selectedTriangle;
    int selectedTriangleIndex;
    float pickTime;

    bool showBVH;

    char filePathBuffer[512];

    // 具身智能 Agent:协调 LLM 推理、工具执行和视觉反馈的闭环
    hhb::core::EmbodiedAIAgent embodiedAgent_;
    hhb::core::GeometryAPI geometryAPI;
    hhb::core::CommandDispatcher commandDispatcher;
    hhb::algorithm::GeometryExpert geometryExpert;
    char userInputBuffer[512];
    
    // 高亮相关
    std::vector<int> highlightIndices;
    std::vector<int> newHighlightIndices;
    HighlightType currentHighlightType;
    bool showHighlight;
    std::atomic<bool> highlightCalculated;
    std::thread calculationThread;
    std::mutex highlightMutex;
    std::string lastAnalysisDesc;

    // 高亮闪烁效果
    float highlightBlinkTimer_;
    bool highlightBlinkState_;

    GLuint loadShader(GLenum type, const char* source);
    
    void processUserInput();
    void highlightParts();
    GLuint createShaderProgram(const char* vertexSource, const char* fragmentSource);

    bool initShaders();
    bool initBuffers();
    void updateVertexData(hhb::core::ObjectPool<hhb::core::Triangle>& pool);
    void updateFPS();
    void buildBVH();
    void handleMouseClick(double xpos, double ypos);
    void screenToRay(double xpos, double ypos, float* rayOrigin, float* rayDirection);

    void initImGui();
    void updateImGui();
    void renderImGui();
    void shutdownImGui();

    void openFileDialog();
    
    bool saveScreenshot(const std::string& filename);
    void captureModelViews(const std::string& modelName);

    void sphericalFibonacciSample(int index, int total, float radius, float* outX, float* outY, float* outZ);
    bool saveFrameAsPNG(const std::string& filename, int w, int h, const std::vector<unsigned char>& pixels);
    void computeViewMatrixFromPosition(float camX, float camY, float camZ,
                                        float targetX, float targetY, float targetZ,
                                        float* outView16);

    bool initLabelShaders();
    void computeFaceCategories();
    void updateLabelVertexData();
    void renderLabelMode();
    void setLabelMode(bool active);
    static void categoryToColor(int categoryId, float* outR, float* outG, float* outB);

    void computeCurvature();
    void computeGeometricFeatures();
    std::vector<int> faceCurvatureIds;
    std::vector<float> faceGaussianCurvature;
    std::vector<float> faceMeanCurvature;
    bool curvatureComputed = false;
    bool geometricFeaturesComputed = false;

    void renderBVH();
    void renderAABB(const hhb::core::Bounds& bounds, const float* color);
};

} // namespace render
} // namespace hhb