Skip to content

Commit

Permalink
add flags that allow both pcd and ply geometries to be centered (#4843)
Browse files Browse the repository at this point in the history
* center geometry by default for ply and not for pcd

* add docs
  • Loading branch information
sashankaryal authored Sep 25, 2024
1 parent 98b27e4 commit 4845d52
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
15 changes: 12 additions & 3 deletions app/packages/looker-3d/src/fo3d/mesh/Ply.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ const PlyWithNoMaterialOverride = ({

export const Ply = ({
name,
ply: { plyPath, preTransformedPlyPath, defaultMaterial, isPcd },
ply: {
plyPath,
preTransformedPlyPath,
defaultMaterial,
isPcd,
centerGeometry,
},
position,
quaternion,
scale,
Expand Down Expand Up @@ -161,15 +167,18 @@ export const Ply = ({
!geometry.attributes.normal?.count
) {
geometry.computeVertexNormals();
geometry.center();

if (centerGeometry) {
geometry.center();
}
}

if (geometry.attributes?.color?.count) {
setIsUsingVertexColors(true);
}

setIsGeometryResolved(true);
}, [geometry]);
}, [geometry, centerGeometry]);

const mesh = useMemo(() => {
if (!isGeometryResolved) {
Expand Down
13 changes: 6 additions & 7 deletions app/packages/looker-3d/src/fo3d/point-cloud/Pcd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { usePcdMaterial } from "./use-pcd-material";

export const Pcd = ({
name,
pcd: { pcdPath, preTransformedPcdPath, defaultMaterial },
pcd: { pcdPath, preTransformedPcdPath, defaultMaterial, centerGeometry },
position,
quaternion,
scale,
Expand All @@ -37,12 +37,11 @@ export const Pcd = ({
// todo: hack until https://github.com/pmndrs/react-three-fiber/issues/245 is fixed
const points = useMemo(() => points_.clone(false), [points_]);

// todo: expose centering of points as an opt-in behavior from the sdk
// useEffect(() => {
// if (points) {
// points.geometry.center();
// }
// }, [points]);
useEffect(() => {
if (points && centerGeometry) {
points.geometry.center();
}
}, [points, centerGeometry]);

const pcdContainerRef = useRef();

Expand Down
12 changes: 8 additions & 4 deletions app/packages/looker-3d/src/hooks/use-fo3d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ export class PcdAsset {
constructor(
readonly pcdPath?: string,
readonly preTransformedPcdPath?: string,
readonly defaultMaterial?: FoPointcloudMaterialProps
readonly defaultMaterial?: FoPointcloudMaterialProps,
readonly centerGeometry?: boolean
) {}
}

Expand All @@ -103,7 +104,8 @@ export class PlyAsset {
readonly plyPath?: string,
readonly preTransformedPlyPath?: string,
readonly defaultMaterial?: FoMeshMaterial,
readonly isPcd?: boolean
readonly isPcd?: boolean,
readonly centerGeometry?: boolean
) {}
}

Expand Down Expand Up @@ -306,7 +308,8 @@ export const useFo3d = (
node["plyPath"],
node["preTransformedPlyPath"],
material as FoMeshMaterial,
node["isPointCloud"] ?? false
node["isPointCloud"] ?? false,
node["centerGeometry"] ?? true
);
}
}
Expand All @@ -315,7 +318,8 @@ export const useFo3d = (
asset = new PcdAsset(
node["pcdPath"],
node["preTransformedPcdPath"],
material as FoPointcloudMaterialProps
material as FoPointcloudMaterialProps,
node["centerGeometry"] ?? false
);
}
} else if (node["_type"].endsWith("Geometry")) {
Expand Down
10 changes: 9 additions & 1 deletion fiftyone/core/threed/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ class PlyMesh(Mesh):
absolute or relative to the directory containing the ``.fo3d`` file
is_point_cloud (bool): whether the PLY file is a point cloud. Defaults
to ``False``
center_geometry (bool): whether to center the geometry. Defaults to
``True``
material (:class:`fiftyone.core.threed.MeshMaterial`, optional):
default material for the mesh if PLY file does not contain
vertex colors. Defaults to
Expand All @@ -291,6 +293,7 @@ def __init__(
name: str,
ply_path: str,
is_point_cloud: bool = False,
center_geometry: bool = True,
default_material: Optional[MeshMaterial] = None,
visible=True,
position: Optional[Vec3UnionType] = None,
Expand All @@ -309,13 +312,18 @@ def __init__(
if not ply_path.lower().endswith(".ply"):
raise ValueError("PLY mesh must be a .ply file")

self.center_geometry = center_geometry
self.ply_path = ply_path
self.is_point_cloud = is_point_cloud

def _to_dict_extra(self):
r = {
**super()._to_dict_extra(),
**{"plyPath": self.ply_path, "isPointCloud": self.is_point_cloud},
**{
"centerGeometry": self.center_geometry,
"plyPath": self.ply_path,
"isPointCloud": self.is_point_cloud,
},
}

if hasattr(self, "_pre_transformed_ply_path"):
Expand Down
5 changes: 5 additions & 0 deletions fiftyone/core/threed/pointcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class PointCloud(Object3D):
the material of the point cloud. If not specified, defaults to a
new instance of :class:`fiftyone.core.threed.PointCloudMaterial`
with its default parameters
center_geometry (bool): whether to center the geometry of the point
cloud. Defaults to ``False``
flag_for_projection (bool): whether to flag the point cloud for
usage in orthographic projection. Each
:class:`fiftyone.core.threed.Scene` can have at most one asset
Expand All @@ -45,6 +47,7 @@ def __init__(
name: str,
pcd_path: str,
material: Optional[PointCloudMaterial] = None,
center_geometry: bool = False,
flag_for_projection: bool = False,
visible=True,
position: Optional[Vec3UnionType] = None,
Expand All @@ -67,6 +70,7 @@ def __init__(
if isinstance(material, dict):
material = PointCloudMaterial._from_dict(material)

self.center_geometry = center_geometry
self.default_material = material or PointCloudMaterial()
self.flag_for_projection = flag_for_projection

Expand All @@ -84,6 +88,7 @@ def set_default_material(self, material: PointCloudMaterial):
def _to_dict_extra(self):
"""Extra properties to include in dictionary representation."""
r = {
"centerGeometry": self.center_geometry,
"pcdPath": self.pcd_path,
"defaultMaterial": self.default_material.as_dict(),
"flagForProjection": self.flag_for_projection,
Expand Down

0 comments on commit 4845d52

Please sign in to comment.