Skip to content

Commit

Permalink
wip: fix ortho projections with bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
ritch committed May 24, 2024
1 parent 127f4c9 commit 8ed2213
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
2 changes: 2 additions & 0 deletions app/packages/looker/src/worker/label-3d-projection-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export const getProjectedCorners = (
[halfDimensions[0], halfDimensions[1], halfDimensions[2]],
];

console.log({ corners });

// rotate first, and translate
const transformedCorners = corners.map((corner) => {
const newRotation = rotation;
Expand Down
47 changes: 32 additions & 15 deletions app/packages/looker/src/worker/threed-label-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ const inferredParamsCache: Record<
OrthogrpahicProjectionMetadata
> = {};

const clamp = (value: number, min: number, max: number) => {
return Math.min(Math.max(value, min), max);
};

const remap = (
value: number,
fromLow: number,
fromHigh: number,
toLow: number,
toHigh: number
) => {
return toLow + ((value - fromLow) * (toHigh - toLow)) / (fromHigh - fromLow);
};

/**
* Use label attributes to infer width, height, and bounds.
*/
Expand Down Expand Up @@ -108,7 +122,8 @@ const PainterFactory3D = (
* Impute bounding box parameters.
*/
Detection: (label: DetectionLabel) => {
const { min_bound, max_bound, normal } = orthographicProjectionParams;
const { min_bound, max_bound, normal, height, width } =
orthographicProjectionParams;
const [xmin, ymin, zmin] = min_bound;
const [xmax, ymax, zmax] = max_bound;

Expand Down Expand Up @@ -136,39 +151,41 @@ const PainterFactory3D = (
// project on xy plane
projectionPlane = "xy";
}

console.log("label.dimensions", label.dimensions);
const { projectedCorners } = getProjectedCorners(box, projectionPlane);

const xRange = xmax - xmin;
const yRange = ymax - ymin;
const zRange = zmax - zmin;
console.log(orthographicProjectionParams);
console.log({ projectedCorners });

const newProjectedCorners = projectedCorners.map(([x, y]) => {
let px, py;

// todo: need to account for negative / positive normals
x = clamp(x, xmin, xmax);
y = clamp(y, ymin, ymax);
// adjust normalization for different projection planes
switch (projectionPlane) {
case "xy":
px = (x - xmin) / xRange;
py = (ymax - y) / yRange;
px = remap(x, xmin, xmax, 1, 0);
py = remap(y, ymin, ymax, 1, 0); // is this right?
break;
case "xz":
px = (x - xmin) / xRange;
py = (zmax - y) / zRange;
px = remap(x, xmin, xmax, 1, 0);
py = remap(y, ymin, ymax, 1, 0); // this change fixed the issue
break;
case "yz":
px = (y - ymin) / yRange;
py = (zmax - x) / zRange;
px = remap(x, ymin, ymax, 1, 0);
py = remap(y, xmin, xmax, 1, 0); // is this right?
break;
}

return [px, py];
});

const convexHullIndices = ch(newProjectedCorners);
console.log(newProjectedCorners);

const convexHullIndices = ch(newProjectedCorners);
const convexHull = convexHullIndices.map((i) => newProjectedCorners[i]);

label.convexHull = convexHull;
// console.log({convexHull})
},
});

Expand Down

0 comments on commit 8ed2213

Please sign in to comment.