-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBinaryPointsLoader.cpp
216 lines (184 loc) · 7.12 KB
/
BinaryPointsLoader.cpp
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
#include "BinaryPointsLoader.h"
#include <osg/Geode>
#include <osg/Point>
#include <osg/PagedLOD>
#include <limits>
using namespace omega;
using namespace cyclops;
///////////////////////////////////////////////////////////////////////////////
// Used by load method
struct LODLevel
{
LODLevel(int dmin, int dmax, int dc) :
distmin(dmin),
distmax(dmax),
dec(dc) {}
int distmin;
int distmax;
int dec;
};
///////////////////////////////////////////////////////////////////////////////
BinaryPointsLoader::BinaryPointsLoader(): ModelLoader("points-binary")
{
osgDB::Registry* reg = osgDB::Registry::instance();
reg->addReaderWriter(new BinaryPointsReader());
}
///////////////////////////////////////////////////////////////////////////////
BinaryPointsLoader::~BinaryPointsLoader()
{
}
///////////////////////////////////////////////////////////////////////////////
bool BinaryPointsLoader::supportsExtension(const String& ext)
{
if(StringUtils::endsWith(ext, "xyzb")) return true;
return false;
}
///////////////////////////////////////////////////////////////////////////////
bool BinaryPointsLoader::load(ModelAsset* model)
{
// Compute max records (points) from source file
String path;
if(!DataManager::findFile(model->info->path, path))
{
ofwarn("BinaryPointsLoader::load: could not find %1%", %model->info->path);
return false;
}
// Default record size = 7 doubles (X,Y,Z,R,G,B,A)
int numFields = 7;
size_t recordSize = sizeof(double)* numFields;
FILE* fin = fopen(path.c_str(), "rb");
// How many records are in the file?
fseek(fin, 0, SEEK_END);
size_t endpos = ftell(fin);
size_t numRecords = endpos / recordSize;
fclose(fin);
// Parse options (format: 'pointsPerBatch dist:dec+')
// where pointsPerBatch is the number of points for each LOD group
// at max LOD, and each distmin:distmax:dec pair is a LOD level with distance from
// eye and decimation level.
Vector<String> args = StringUtils::split(model->info->options, " ");
size_t pointsPerBatch = boost::lexical_cast<size_t>(args[0]);
// Convert points per batch to batch length as file size percentage.
size_t lengthP = pointsPerBatch * BINARY_POINTS_MAX_BATCHES / numRecords;
ofmsg("[BinaryPointsLoader] Total Points: <%1%> Points per batch: <%2%> Batch length(%%): <%3%>",
%numRecords
%pointsPerBatch
%lengthP);
if(lengthP < 1)
{
lengthP = 1;
pointsPerBatch = numRecords / BINARY_POINTS_MAX_BATCHES;
ofwarn("Can't have more than %1% batches. Adjusting batch size to %2%", %BINARY_POINTS_MAX_BATCHES %pointsPerBatch);
}
int mindec = 1000000;
Vector<LODLevel> lodlevels;
for(int i = 1; i < args.size(); i++)
{
Vector<String> lodargs = StringUtils::split(args[i], ":");
LODLevel ll(
boost::lexical_cast<int>(lodargs[0]),
boost::lexical_cast<int>(lodargs[1]),
boost::lexical_cast<int>(lodargs[2])
);
lodlevels.push_back(ll);
if(ll.dec < mindec) mindec = ll.dec;
}
oflog(Verbose, "[BinaryPointsLoader] LOD levels: <%1%>", %lodlevels.size());
// Create root group for this point cloud
Ref<osg::Group> group = new osg::Group();
// get base filename (without extension)
String basename;
String extension;
StringUtils::splitBaseFilename(model->info->path, basename, extension);
float maxf = numeric_limits<float>::max();
float minf = -numeric_limits<float>::max();
Vector4f rgbamin = Vector4f(maxf, maxf, maxf, maxf);
Vector4f rgbamax = Vector4f(minf, minf, minf, minf);
// Iterate for each batch
for(int startP = 0; startP <= BINARY_POINTS_MAX_BATCHES; startP += lengthP)
{
int childid = 0;
osg::PagedLOD* plod = new osg::PagedLOD();
plod->setRangeMode(osg::LOD::DISTANCE_FROM_EYE_POINT);
plod->setNumChildrenThatCannotBeExpired(2);
group->addChild(plod);
osgDB::Options* options = new osgDB::Options;
options->setOptionString(ostr("xyzrgba -b %1%", %(pointsPerBatch / mindec)));
plod->setDatabaseOptions(options);
//plod->setCenterMode(osg::LOD::USE_BOUNDING_SPHERE_CENTER);
// Create LOD groups for each batch
String filename;
foreach(LODLevel ll, lodlevels)
{
filename = ostr("%1%.%2%-%3%-%4%.xyzb",
%basename
%startP %lengthP %ll.dec);
plod->setFileName(childid, filename);
plod->setRange(childid, ll.distmin, ll.distmax);
// Minimum expiration time and frames.
plod->setMinimumExpiryFrames(childid, 60);
plod->setMinimumExpiryTime(childid, 5);
if(childid == 0)
{
// Load or compute bounds
Ref<osgDB::Options> boundoptions = new osgDB::Options;
boundoptions->setOptionString(ostr("xyzrgba -b %1% -z", %(pointsPerBatch / mindec)));
Ref<osg::Node> n = osgDB::readNodeFile(filename, boundoptions);
// The node only stores user data. read it back.
float brgbamin[4];
float brgbamax[4];
float bpointmin[3];
float bpointmax[3];
n->getUserValue("xmin", bpointmin[0]);
n->getUserValue("xmax", bpointmax[0]);
n->getUserValue("ymin", bpointmin[1]);
n->getUserValue("ymax", bpointmax[1]);
n->getUserValue("zmin", bpointmin[2]);
n->getUserValue("zmax", bpointmax[2]);
n->getUserValue("rmin", brgbamin[0]);
n->getUserValue("rmax", brgbamax[0]);
n->getUserValue("gmin", brgbamin[1]);
n->getUserValue("gmax", brgbamax[1]);
n->getUserValue("bmin", brgbamin[2]);
n->getUserValue("bmax", brgbamax[2]);
n->getUserValue("amin", brgbamin[3]);
n->getUserValue("amax", brgbamax[3]);
// Use this batch bounds to upate the point cloud bounds
for(int j = 0; j < 4; j++)
{
if(brgbamin[j] < (rgbamin)[j]) (rgbamin)[j] = brgbamin[j];
if(brgbamax[j] > (rgbamax)[j]) (rgbamax)[j] = brgbamax[j];
}
// Compute batch center
Vector3f bm(bpointmin[0], bpointmin[1], bpointmin[2]);
Vector3f bM(bpointmax[0], bpointmax[1], bpointmax[2]);
Vector3f bcenter = (bm + bM)/2;
//ofmsg("batch center: %1%", %bcenter);
plod->setCenter(osg::Vec3d(bcenter[0], bcenter[1], bcenter[2]));
}
childid++;
}
}
// Save loaded results in the model info
string output =
ostr("{ "
"'minR': %f, 'maxR': %f, "
"'minG': %f, 'maxG': %f, "
"'minB': %f, 'maxB': %f, "
"'minA': %f, 'maxA': %f }",
%rgbamin[0] %rgbamax[0]
%rgbamin[1] %rgbamax[1]
%rgbamin[2] %rgbamax[2]
%rgbamin[3] %rgbamax[3]
);
oflog(Verbose, "[BinaryPointsLoader] model info: <%1%>", %output);
model->info->loaderOutput = output;
model->nodes.push_back(group);
// if(node)
// {
// node->getUserValue("loaderOutput", model->info->loaderOutput);
//model->nodes.push_back(node);
// return true;
// }
return true;
}