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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "voronoi_device.h"
#include <functional>
#include <queue>
namespace embree {
RTCScene g_scene;
TutorialData data;
/* scene data */
#define NUM_COLORS 27
int g_num_points_current;
typedef void (*DrawGUI)(void);
extern "C" Vec3fa g_query_point;
extern "C" int g_num_points;
extern "C" int g_num_knn;
extern "C" bool g_show_voronoi;
extern "C" bool g_point_repulsion;
extern "C" float g_tmax;
struct Neighbour
{
unsigned int primID;
float d;
bool operator<(Neighbour const& n1) const { return d < n1.d; }
};
struct KNNResult
{
KNNResult(int num_knn, Point const * const points)
: points(points)
{
visited.reserve(2 * num_knn);
}
unsigned int k;
std::priority_queue<Neighbour, std::vector<Neighbour>> knn;
std::vector<unsigned int> visited; // primIDs of all visited points
Point const * const points;
};
// ======================================================================== //
// User defined point geometry //
// ======================================================================== //
struct Point
{
ALIGNED_STRUCT_(16)
Vec3fa p; //!< position
RTCGeometry geometry;
unsigned int geomID;
};
void pointBoundsFunc(const struct RTCBoundsFunctionArguments* args)
{
const Point* points = (const Point*) args->geometryUserPtr;
RTCBounds* bounds_o = args->bounds_o;
const Point& point = points[args->primID];
bounds_o->lower_x = point.p.x;
bounds_o->lower_y = point.p.y;
bounds_o->lower_z = point.p.z;
bounds_o->upper_x = point.p.x;
bounds_o->upper_y = point.p.y;
bounds_o->upper_z = point.p.z;
}
bool pointQueryFunc(struct RTCPointQueryFunctionArguments* args)
{
RTCPointQuery* query = (RTCPointQuery*)args->query;
assert(args->query);
KNNResult* result = (KNNResult*)args->userPtr;
assert(result);
const unsigned int primID = args->primID;
const Vec3f q(query->x, query->y, query->z);
const Point& point = result->points[primID];
const float d = distance(point.p, q);
result->visited.push_back(primID);
if (d < query->radius && (result->knn.size() < result->k || d < result->knn.top().d))
{
Neighbour neighbour;
neighbour.primID = primID;
neighbour.d = d;
if (result->knn.size() == result->k)
result->knn.pop();
result->knn.push(neighbour);
if (result->knn.size() == result->k)
{
const float R = result->knn.top().d;
query->radius = R;
return true;
}
}
return false;
}
void knnQuery(Vec3f const& q, float radius, KNNResult* result)
{
RTCPointQuery query;
query.x = q.x;
query.y = q.y;
query.z = q.z;
query.radius = radius;
query.time = 0.f;
RTCPointQueryContext context;
rtcInitPointQueryContext(&context);
rtcPointQuery(data.scene, &query, &context, pointQueryFunc, (void*)result);
}
void createPoints (TutorialData& data)
{
RTCGeometry geom = rtcNewGeometry(g_device, RTC_GEOMETRY_TYPE_USER);
data.points = (Point*) alignedMalloc(data.num_points*sizeof(Point), 16);
data.points_tmp = (Point*) alignedMalloc(data.num_points*sizeof(Point), 16);
unsigned int geomID = rtcAttachGeometry(data.scene, geom);
for (unsigned int i=0; i<data.num_points; i++) {
data.points[i].geometry = geom;
data.points[i].geomID = geomID;
data.points_tmp[i].geometry = geom;
data.points_tmp[i].geomID = geomID;
}
rtcSetGeometryUserPrimitiveCount(geom, data.num_points);
rtcSetGeometryUserData(geom, data.points);
rtcSetGeometryBoundsFunction(geom, pointBoundsFunc, nullptr);
rtcCommitGeometry(geom);
rtcReleaseGeometry(geom);
RandomSampler rs;
RandomSampler_init(rs, 42);
for (unsigned int i = 0; i < data.num_points; ++i)
{
float xi1 = RandomSampler_getFloat(rs);
float xi2 = RandomSampler_getFloat(rs);
data.points[i].p = Vec3f(xi1, 0.f, xi2);
}
g_num_points_current = data.num_points;
}
/* called by the C++ code for initialization */
extern "C" void device_init (char* cfg)
{
/* create scene */
TutorialData_Constructor(&data);
data.query_point = g_query_point;
data.num_points = g_num_points;
data.num_knn = g_num_knn;
data.show_voronoi = g_show_voronoi;
data.point_repulsion = g_point_repulsion;
data.tmax = g_tmax;
g_scene = data.scene = rtcNewScene(g_device);
createPoints(data);
rtcCommitScene(data.scene);
data.colors = (Vec3fa*) alignedMalloc(NUM_COLORS*sizeof(Point), 16);
for (int r = 0; r < 3; ++r) for (int g = 0; g < 3; ++g) for (int b = 0; b < 3; ++b)
data.colors[r * 9 + g * 3 + b] = Vec3fa(0.2f + 0.3f * r, 0.2f + 0.3f * g, 0.2f + 0.3f * b);
}
/* renders a single screen tile */
void renderTileStandard(int taskIndex,
int threadIndex,
int* pixels,
const unsigned int width,
const unsigned int height,
const float time,
const ISPCCamera& camera,
const int numTilesX,
const int numTilesY)
{
const unsigned int tileY = taskIndex / numTilesX;
const unsigned int tileX = taskIndex - tileY * numTilesX;
const unsigned int x0 = tileX * TILE_SIZE_X;
const unsigned int x1 = min(x0+TILE_SIZE_X,width);
const unsigned int y0 = tileY * TILE_SIZE_Y;
const unsigned int y1 = min(y0+TILE_SIZE_Y,height);
for (unsigned int y=y0; y<y1; y++) for (unsigned int x=x0; x<x1; x++)
{
Vec3fa color = Vec3fa(0.f);
if (data.show_voronoi)
{
Vec3fa q = Vec3fa((float(x) + 0.5f) / width, 0.f, (float(y) + 0.5f) / height);
KNNResult result(data.num_knn, data.points);
result.k = 1;
knnQuery(q, data.tmax, &result);
unsigned int primID = result.knn.empty() ? RTC_INVALID_GEOMETRY_ID : result.knn.top().primID;
if (primID != RTC_INVALID_GEOMETRY_ID)
color = data.colors[primID % 27];
}
/* write color to framebuffer */
unsigned int r = (unsigned int) (255.0f * clamp(color.x,0.0f,1.0f));
unsigned int g = (unsigned int) (255.0f * clamp(color.y,0.0f,1.0f));
unsigned int b = (unsigned int) (255.0f * clamp(color.z,0.0f,1.0f));
pixels[y*width+x] = (b << 16) + (g << 8) + r;
}
}
/* task that renders a single screen tile */
void renderTileTask (int taskIndex, int threadIndex, int* pixels,
const unsigned int width,
const unsigned int height,
const float time,
const ISPCCamera& camera,
const int numTilesX,
const int numTilesY)
{
renderTileStandard(taskIndex,threadIndex,pixels,width,height,time,camera,numTilesX,numTilesY);
}
void splat_color(int* pixels,
unsigned int width,
unsigned int height,
int kernel_size,
float x, float y, Vec3fa const& color)
{
for (int dy = -kernel_size; dy <= kernel_size; ++dy)
for (int dx = -kernel_size; dx <= kernel_size; ++dx)
{
unsigned int r = (unsigned int)(255.0f * clamp(color.x, 0.0f, 1.0f));
unsigned int g = (unsigned int)(255.0f * clamp(color.y, 0.0f, 1.0f));
unsigned int b = (unsigned int)(255.0f * clamp(color.z, 0.0f, 1.0f));
const unsigned int px = (unsigned int)min(width - 1.f, max(0.f, x * width + dx));
const unsigned int py = (unsigned int)min(height - 1.f, max(0.f, y * height + dy));
pixels[py*width + px] = (b << 16) + (g << 8) + r;
}
}
void rebuild_bvh()
{
rtcReleaseScene (data.scene);
g_scene = data.scene = rtcNewScene(g_device);
RTCGeometry geom = rtcNewGeometry(g_device, RTC_GEOMETRY_TYPE_USER);
rtcAttachGeometry(data.scene, geom);
rtcSetGeometryUserPrimitiveCount(geom, data.num_points);
rtcSetGeometryUserData(geom, data.points);
rtcSetGeometryBoundsFunction(geom, pointBoundsFunc, nullptr);
rtcCommitGeometry(geom);
rtcReleaseGeometry(geom);
rtcCommitScene(data.scene);
}
extern "C" void renderFrameStandard (int* pixels,
const unsigned int width,
const unsigned int height,
const float time,
const ISPCCamera& camera)
{
}
/* called by the C++ code to render */
extern "C" void device_render (int* pixels,
const unsigned int width,
const unsigned int height,
const float time,
const ISPCCamera& camera)
{
data.query_point = g_query_point;
data.num_points = g_num_points;
data.num_knn = g_num_knn;
data.show_voronoi = g_show_voronoi;
data.point_repulsion = g_point_repulsion;
data.tmax = g_tmax;
if (data.num_points != g_num_points_current)
{
rtcReleaseScene (data.scene);
g_scene = data.scene = rtcNewScene(g_device);
alignedFree(data.points);
alignedFree(data.points_tmp);
createPoints(data);
rtcCommitScene(data.scene);
}
if (data.point_repulsion)
{
parallel_for(size_t(0), size_t(data.num_points), [&](const range<size_t>& range) {
for (size_t i = range.begin(); i < range.end(); i++)
{
// perform nearest neighbour search for point
KNNResult result(data.num_knn, data.points);
result.k = data.num_knn + 1;
knnQuery(data.points[i].p, data.tmax, &result);
if (result.knn.empty())
continue;
const float D = result.knn.top().d;
result.knn.pop();
// store number of nearest neighbours for normalization later
const size_t K = result.knn.size();
assert(K >= 1);
// tusk point repulsion formula
Vec3fa dx(0.f);
while (!result.knn.empty())
{
Point const& q = data.points[result.knn.top().primID];
dx += (data.points[i].p - q.p) * (D / (result.knn.top().d + 1e-4f) - 1.f);
result.knn.pop();
}
data.points_tmp[i].p = min(Vec3fa(1.f), max(Vec3fa(0.f), data.points[i].p + (1.f / K) * dx));
}
});
// copy new point locations and rebuild bvh
for (int i = 0; i < data.num_points; ++i)
data.points[i] = data.points_tmp[i];
rebuild_bvh();
}
// clear image and draw voronoi regions if enabled
const int numTilesX = (width + TILE_SIZE_X - 1) / TILE_SIZE_X;
const int numTilesY = (height + TILE_SIZE_Y - 1) / TILE_SIZE_Y;
parallel_for(size_t(0), size_t(numTilesX*numTilesY), [&](const range<size_t>& range) {
const int threadIndex = (int)TaskScheduler::threadIndex();
for (size_t i = range.begin(); i < range.end(); i++)
renderTileTask((int)i, threadIndex, pixels, width, height, time, camera, numTilesX, numTilesY);
});
// draw points
parallel_for(size_t(0), size_t(data.num_points), [&](const range<size_t>& range) {
for (size_t i = range.begin(); i < range.end(); i++)
{
Point const& p = data.points[i];
Vec3fa color = Vec3fa(0.9f);
if (data.show_voronoi) color = data.colors[i%NUM_COLORS] / 0.8f;
splat_color(pixels, width, height, 2, p.p.x, p.p.z, color);
}
});
if (!data.show_voronoi)
{
// perform nearest neighbour query for query point
KNNResult result(data.num_knn, data.points);
result.k = data.num_knn;
knnQuery(data.query_point, data.tmax, &result);
if (!result.knn.empty())
{
// draw search radius
parallel_for(size_t(0), size_t(numTilesX*numTilesY), [&](const range<size_t>& range) {
for (size_t i = range.begin(); i < range.end(); i++)
{
const unsigned int tileY = (unsigned int)i / numTilesX;
const unsigned int tileX = (unsigned int)i - tileY * numTilesX;
const unsigned int x0 = tileX * TILE_SIZE_X;
const unsigned int x1 = min(x0 + TILE_SIZE_X, width);
const unsigned int y0 = tileY * TILE_SIZE_Y;
const unsigned int y1 = min(y0 + TILE_SIZE_Y, height);
for (unsigned int y = y0; y < y1; y++)
for (unsigned int x = x0; x < x1; x++)
{
Vec3fa q = Vec3fa((float(x) + 0.5f) / width, 0.f, (float(y) + 0.5f) / height);
if ( pixels[y*width + x] > 0)
continue;
Vec3fa color(0.0f, 0.0f, 0.0f);
if (distance(q, data.query_point) < result.knn.top().d)
color = Vec3fa(0.2f, 0.2f, 0.8f);
else if (distance(q, data.query_point) < data.tmax)
color = Vec3fa(0.2f, 0.2f, 0.2f);
else {
continue;
}
unsigned int r = (unsigned int)(255.0f * clamp(color.x, 0.0f, 1.0f));
unsigned int g = (unsigned int)(255.0f * clamp(color.y, 0.0f, 1.0f));
unsigned int b = (unsigned int)(255.0f * clamp(color.z, 0.0f, 1.0f));
pixels[y*width + x] = (b << 16) + (g << 8) + r;
}
}
});
}
// draw all visited points
for (unsigned int primID : result.visited)
{
Point const& p = data.points[primID];
splat_color(pixels, width, height, 2, p.p.x, p.p.z, Vec3fa(0.8f, 0.2f, 0.2f));
}
// draw nearest neighbours
while (!result.knn.empty())
{
Point const& p = data.points[result.knn.top().primID];
splat_color(pixels, width, height, 2, p.p.x, p.p.z, Vec3fa(0.2f, 0.8f, 0.2f));
result.knn.pop();
}
// draw query point
splat_color(pixels, width, height, 2, data.query_point.x, data.query_point.z, Vec3fa(0.8f, 0.8f, 0.2f));
}
}
/* called by the C++ code for cleanup */
extern "C" void device_cleanup ()
{
rtcReleaseDevice(g_device); g_device = nullptr;
TutorialData_Destructor(&data);
}
} // namespace embree
|