Skip to content

Commit d3470f4

Browse files
Export OcTree (#417)
* Update exprts * (transpiler) read revision correctly * Fix typo * Make transpiler more robust More robust to whitespace; fix dot bug (dots are any character in regex); Arguments are optional to constructors * OccupancyMap* -> OcTree* Rename as to similar to OccupancyGrid*; Including seperation of all classes to their own file, needed by transpiler; Remove all seperate class property definitions; not supported by transpiler Co-authored-by: Peter Sari <[email protected]>
1 parent 7fc21e0 commit d3470f4

File tree

7 files changed

+317
-316
lines changed

7 files changed

+317
-316
lines changed

es6-support/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export * from './models/TriangleList'
2323

2424
export * from './navigation/OccupancyGrid'
2525
export * from './navigation/OccupancyGridClient'
26+
export * from './navigation/OcTree'
27+
export * from './navigation/ColorOcTree'
28+
export * from './navigation/OcTreeClient'
2629
export * from './navigation/Odometry'
2730
export * from './navigation/Path'
2831
export * from './navigation/Point'

es6-transpiler.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,18 @@ const transpile = {
206206
// Replace initial ROS3D assignment
207207
initialROS3DAssignment: [
208208
// from
209-
/var ROS3D = ROS3D \|\| \{\n REVISION \: '0.18.0'\n\};/m,
209+
/var ROS3D = ROS3D \|\| \{\n REVISION \: '([0-9]+\.[0-9]+\.[0-9]+)'\n\};/m,
210210
// to
211-
`export var REVISION = '0.18.0';`,
211+
(match, $1) => {
212+
const revision = $1
213+
return `export var REVISION = '${revision}';`
214+
},
212215
],
213216
// Replace mutations with exported properties
214-
exportedProperites: (filepath) => [
217+
exportedProperties: (filepath) => [
215218
// from:
216219
// ROS3D.MARKER_ARROW = 0;
217-
/\nROS3D\.(.*)\s+?=\s+?(.*)/g,
220+
/\nROS3D\.(.*)\s*=\s*(.*)/g,
218221
// to:
219222
// export var MARKER_ARROW = 0;
220223
(match, $1, $2) => {
@@ -268,7 +271,7 @@ const transpile = {
268271
buildInheritanceIndexViaProto: [
269272
// from:
270273
// ROS3D.PoseWithCovariance.prototype.__proto__ = THREE.Object3D.prototype;
271-
/ROS3D.(\w+).prototype.__proto__ = (.*).prototype;[\r\n]?/g,
274+
/ROS3D\.(\w+)\.prototype\.__proto__ = (.*)\.prototype;[\r\n]?/g,
272275
// to:
273276
// set PoseWithCovariance to subclass from THREE.Object3D in inheritance index
274277
(match, $1, $2) => {
@@ -282,7 +285,7 @@ const transpile = {
282285
buildInheritanceIndexViaObjectAssign: [
283286
// from:
284287
// Object.assign(InteractiveMarker.prototype, THREE.EventDispatcher.prototype);
285-
/Object.assign\((\w+).prototype, (.*).prototype\);/g,
288+
/Object\.assign\((\w+)\.prototype,\s*(.*)\.prototype\);/g,
286289
// to:
287290
// set InteractiveMarker to subclass from THREE.EventDispatcher in inheritance index
288291
(match, $1, $2) => {
@@ -295,8 +298,8 @@ const transpile = {
295298
// Refactor methods
296299
methods: [
297300
// from:
298-
// ROS3D.Arrow2.prototype.dispose = function() { ... };
299-
/ROS3D.(\w+).prototype.(\w+) = function|function\s+?(\w+)/g,
301+
// ROS3D.Arrow2.prototype.dispose = function () { ... };
302+
/ROS3D\.(\w+)\.prototype\.(\w+)\s*=\s*function\s*|function\s+(\w+)/g,
300303
// to:
301304
// dispose() { ... };
302305
(match, $1, $2, $3) => {
@@ -324,10 +327,10 @@ const transpile = {
324327
constructors: (filepath, state = { foundConstructor: false }) => [
325328
// from:
326329
// ROS3D.Arrow2 = function(options) { ... };
327-
/ROS3D.(\w+)\s*=\s*function/g,
330+
/ROS3D\.(\w+)\s*=\s*function\s*\((.*)\)/g,
328331
// to:
329332
// constructor(options) { ... };
330-
(match, $1) => {
333+
(match, $1, $2) => {
331334
const isClass = isFileClass(filepath, $1)
332335
// if (isClass1 !== isClass2) {
333336
// logWarning('class mismatch', {
@@ -339,13 +342,14 @@ const transpile = {
339342
// }
340343
if (isClass) {
341344
if (state.foundConstructor) {
342-
logError('already found a constructor in this file...', { match, $1 })
345+
logError('Already found a constructor in this file...', { match, $1, $2 })
343346
}
344347
state.foundConstructor = true
345348
if (debugRules.logConstructors) {
346-
logInfo('found constructor', { match, $1 })
349+
logInfo('Found constructor', { match, $1, $2 })
347350
}
348-
return 'constructor'
351+
const arguments = $2
352+
return `constructor(${arguments})`
349353
} else {
350354
return match
351355
}
@@ -434,7 +438,7 @@ const transpile = {
434438
// }
435439
// /.*(\*\/).*|[\r\n]+$(?:[\r\n]+$)+((?![\r\n]+))|.*/gm,
436440
// /(\/\*\*(?:$|[.\r\n])*\*\/(?:$|[\s\r\n])*constructor\(.*)|[\r\n]+$(?:[\r\n]+$)+((?![\r\n]+))|.*/gm,
437-
/((?:\/\*\*(?:(?:\*[^/]|[^*])+?)\*\/)(?:[\s\r\n])*constructor\(.*)|$(?:[\r\n]$)*((?![\r\n]))|.+/gm,
441+
/((?:\/\*\*(?:(?:\*[^/]|[^*])+?)\*\/)(?:[\s\r\n])*constructor\s*\(.*)|$(?:[\r\n]$)*((?![\r\n]))|.+/gm,
438442
// to:
439443
// export class Arrow2 extends THREE.ArrowHelper {
440444
// constructor(options) {
@@ -671,7 +675,7 @@ const transpileToEs6 = function (content, filepath, grunt) {
671675
const transpileConstructors = transpile.constructors(filepath)
672676
const transpileSuperCalls = transpile.superCalls(filepath)
673677
const transpileClasses = transpile.classes(filepath)
674-
const transpileExportedProperites= transpile.exportedProperites(filepath)
678+
const transpileExportedProperties= transpile.exportedProperties(filepath)
675679

676680
return transpiled
677681
.replace(...transpileInternalDependencies)
@@ -682,7 +686,7 @@ const transpileToEs6 = function (content, filepath, grunt) {
682686
.replace(...transpileConstructors)
683687
.replace(...transpileSuperCalls)
684688
.replace(...transpileClasses)
685-
.replace(...transpileExportedProperites)
689+
.replace(...transpileExportedProperties)
686690
}
687691

688692
// Injects es6 imports based on dependency and export

src/navigation/ColorOcTree.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @author Peter Sari - [email protected]
3+
*/
4+
5+
ROS3D.ColorOcTree = function(options) {
6+
ROS3D.OcTree.call(this, options);
7+
this.useOwnColor = (typeof options.palette !== 'undefined') && options.colorMode === ROS3D.OcTreeColorMode.COLOR;
8+
};
9+
10+
ROS3D.ColorOcTree.prototype.__proto__ = ROS3D.OcTree.prototype;
11+
12+
ROS3D.ColorOcTree.prototype._readNodeData = function (dataStream, node) {
13+
node.value = dataStream.readFloat32(); // occupancy
14+
node.color = {
15+
r: dataStream.readUint8(), // red
16+
g: dataStream.readUint8(), // green
17+
b: dataStream.readUint8(), // blue
18+
};
19+
20+
};
21+
22+
ROS3D.ColorOcTree.prototype._obtainColor = function (node) {
23+
if (!this.useOwnColor) { return ROS3D.OcTree.prototype._obtainColor.call(this, node); }
24+
return node.color;
25+
};

src/navigation/OcTree.js

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @author Peter Sari - [email protected]
3+
*/
4+
5+
/**
6+
* Toggles voxel visibility
7+
*
8+
* * `occupied` - only voxels that are above or equal to the occupation threshold are shown
9+
* * `free` - only voxels that are below the occupation threshold are shown
10+
* * `all` - all allocated voxels are shown
11+
*/
12+
ROS3D.OcTreeVoxelRenderMode = {
13+
OCCUPIED: 'occupied',
14+
FREE: 'free',
15+
ALL: 'all',
16+
};
17+
18+
/**
19+
* Coloring modes for each voxel
20+
*
21+
* * 'solid' - voxels will have a single solid color set by the tree globally
22+
* * 'occupancy' - voxels are false colored by their occupancy value. Fall back for `solid` if not available.
23+
* * 'color' - voxels will colorized by their
24+
*/
25+
ROS3D.OcTreeColorMode = {
26+
SOLID: 'solid',
27+
OCCUPANCY: 'occupancy',
28+
COLOR: 'color'
29+
};
30+
31+
// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
32+
33+
/**
34+
* Specilaization of BaseOcTree
35+
*
36+
* @constructor
37+
* @param options - object with following keys:
38+
* * inherited from BaseOctree
39+
* * occupancyThreshold (optional) - threshold value that separates occupied and free voxels from each other. (Default: 0)
40+
* * colorMode (optional) - Coloring mode @see ROS3D.OcTreeColorMode.
41+
* * palette (optional) - Palette used for false-coloring (default: predefined palette)
42+
* * paletteSclae (optional) - Scale of palette to represent a wider range of values (default: 1.)
43+
*/
44+
45+
ROS3D.OcTree = function(options) {
46+
ROS3D.OcTreeBase.call(this, options);
47+
48+
this._defaultOccupiedValue = 1.;
49+
this._defaultFreeValue = -1.;
50+
51+
this.occupancyThreshold = (typeof options.occupancyThreshold !== 'undefined') ? options.occupancyThreshold : 0.0000001;
52+
53+
this.useFlatColoring = (typeof options.colorMode !== 'undefined') && options.colorMode === ROS3D.OcTreeColorMode.SOLID;
54+
55+
this.palette = (typeof options.palette !== 'undefined') ? options.palette.map(color => new THREE.Color(color)) :
56+
[
57+
{ r: 0, g: 0, b: 128, }, // dark blue (low)
58+
{ r: 0, g: 255, b: 0, }, // green
59+
{ r: 255, g: 255, b: 0, }, // yellow (mid)
60+
{ r: 255, g: 128, b: 0, }, // orange
61+
{ r: 255, g: 0, b: 0, } // red (high)
62+
];
63+
64+
this.paletteScale = (typeof options.paletteScale !== 'undefined') ? options.paletteScale : 1.;
65+
};
66+
67+
ROS3D.OcTree.prototype.__proto__ = ROS3D.OcTreeBase.prototype;
68+
69+
ROS3D.OcTree.prototype._readNodeData = function (dataStream, node) {
70+
node.value = dataStream.readFloat32();
71+
};
72+
73+
ROS3D.OcTree.prototype._obtainColor = function (node) {
74+
if (this.useFlatColoring) {
75+
return this.color;
76+
}
77+
78+
// Use a simple sigmoid curve to fit values from -inf..inf into 0..1 range
79+
const value = 1. / (1. + Math.exp(-node.value * this.paletteScale)) * this.palette.length; // Normalize
80+
81+
const intVal = Math.trunc(value);
82+
const fracVal = value - intVal;
83+
84+
if (intVal < 0) { return this.palette[0]; }
85+
if (intVal >= this.palette.length - 1) { return this.palette[this.palette.length - 1]; }
86+
87+
// Simple lerp
88+
return {
89+
r: fracVal * this.palette[intVal].r + (1. - fracVal) * this.palette[intVal + 1].r,
90+
g: fracVal * this.palette[intVal].g + (1. - fracVal) * this.palette[intVal + 1].g,
91+
b: fracVal * this.palette[intVal].b + (1. - fracVal) * this.palette[intVal + 1].b,
92+
};
93+
94+
};
95+
96+
ROS3D.OcTree.prototype._checkOccupied = function (node) {
97+
return node.value >= this.occupancyThreshold;
98+
};

0 commit comments

Comments
 (0)