Skip to content

Commit

Permalink
Add scale and offset args for pointcloud registration
Browse files Browse the repository at this point in the history
  • Loading branch information
j9ac9k committed Mar 1, 2024
1 parent 94e1589 commit ca9a9a6
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
99 changes: 98 additions & 1 deletion src/codem/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import warnings
from contextlib import ContextDecorator
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Tuple
from typing import Union

import yaml
from codem import __version__
Expand Down Expand Up @@ -74,6 +74,12 @@ class CodemRunConfig:
ICP_RMSE_THRESHOLD: float = 0.0001
ICP_ROBUST: bool = True
ICP_SOLVE_SCALE: bool = True
OFFSET_X: Union[str, int] = 'auto'
OFFSET_Y: Union[str, int] = 'auto'
OFFSET_Z: Union[str, int] = 'auto'
SCALE_X: Union[str, float] = 0.01
SCALE_Y: Union[str, float] = 0.01
SCALE_Z: Union[str, float] = 0.01
VERBOSE: bool = False
ICP_SAVE_RESIDUALS: bool = False
OUTPUT_DIR: Optional[str] = None
Expand Down Expand Up @@ -137,6 +143,28 @@ def __post_init__(self) -> None:
raise ValueError(
"ICP minimum change in RMSE convergence threshold must be greater than 0."
)
for offset in [self.OFFSET_X, self.OFFSET_Y, self.OFFSET_Z]:
if (
isinstance(offset, str)
and offset != "auto"
and not offset.isnumeric
):
raise ValueError(
"Offset values need to be set to 'auto' or an integer"
)
if offset != 'auto':
offset = int(offset)
for scale in [self.SCALE_X, self.SCALE_Y, self.SCALE_Z]:
if (
isinstance(scale, str)
and scale != "auto"
):
try:
scale = float(scale)
except ValueError as e:
raise ValueError(
"Offset values need to be set to 'auto' or an float"
) from e

# dump config
config_path = os.path.join(self.OUTPUT_DIR, "config.yml")
Expand Down Expand Up @@ -272,6 +300,69 @@ def get_args() -> argparse.Namespace:
action="store_true",
help="Write ICP residual information",
)
ap.add_argument(
"--offset-x",
type=str,
default='auto',
help=(
"Offset to be subtracted from the X nominal value, before "
"the value is scaled. The special value auto can be specified, which causes "
"the writer to set the offset to the minimum value of the dimension. "
)
)
ap.add_argument(
"--offset-y",
type=str,
default='auto',
help=(
"Offset to be subtracted from the Y nominal value, before "
"the value is scaled. The special value auto can be specified, which causes "
"the writer to set the offset to the minimum value of the dimension. "
)
)
ap.add_argument(
"--offset-z",
type=str,
default='auto',
help=(
"Offset to be subtracted from the Z nominal value, before "
"the value is scaled. The special value auto can be specified, which causes "
"the writer to set the offset to the minimum value of the dimension. "
)
)
ap.add_argument(
"--scale-x",
type=str,
default='.01',
help=(
"Scale to be divided from the X nominal value, "
"after the offset has been applied. The special value auto can be specified, "
"which causes the writer to select a scale to set the stored values of the "
"dimensions to range from [0, 2147483647]."
)
)
ap.add_argument(
"--scale-y",
type=str,
default='.01',
help=(
"Scale to be divided from the Y nominal value, "
"after the offset has been applied. The special value auto can be specified, "
"which causes the writer to select a scale to set the stored values of the "
"dimensions to range from [0, 2147483647]."
)
)
ap.add_argument(
"--scale-z",
type=str,
default='.01',
help=(
"Scale to be divided from the Z nominal value, "
"after the offset has been applied. The special value auto can be specified, "
"which causes the writer to select a scale to set the stored values of the "
"dimensions to range from [0, 2147483647]."
)
)
ap.add_argument(
"--verbose", "-v", action="store_true", help="turn on verbose logging"
)
Expand Down Expand Up @@ -327,6 +418,12 @@ def create_config(args: argparse.Namespace) -> CodemParameters:
ICP_RMSE_THRESHOLD=float(args.icp_rmse_threshold),
ICP_ROBUST=args.icp_robust,
ICP_SOLVE_SCALE=args.icp_solve_scale,
SCALE_X=args.scale_x,
SCALE_Y=args.scale_y,
SCALE_Z=args.scale_z,
OFFSET_X=args.offset_x,
OFFSET_Y=args.offset_y,
OFFSET_Z=args.offset_z,
VERBOSE=args.verbose,
ICP_SAVE_RESIDUALS=args.icp_save_residuals,
TIGHT_SEARCH=args.tight_search,
Expand Down
8 changes: 7 additions & 1 deletion src/codem/preprocessing/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import math
import os
import tempfile
from typing import Any
from typing import Dict
from typing import Optional
from typing import Tuple
from typing import Union

import codem.lib.resources as r
import cv2
Expand Down Expand Up @@ -70,6 +70,12 @@ class CodemParameters(TypedDict):
ICP_RMSE_THRESHOLD: float
ICP_ROBUST: bool
ICP_SOLVE_SCALE: bool
OFFSET_X: Union[str, int]
OFFSET_Y: Union[str, int]
OFFSET_Z: Union[str, int]
SCALE_X: Union[str, float]
SCALE_Y: Union[str, float]
SCALE_Z: Union[str, float]
VERBOSE: bool
ICP_SAVE_RESIDUALS: bool
OUTPUT_DIR: str
Expand Down
7 changes: 7 additions & 0 deletions src/codem/registration/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,13 @@ def _apply_pointcloud(self) -> None:
writer_kwargs = {"filename": self.out_name}
if self.fnd_crs is not None:
writer_kwargs["a_srs"] = self.fnd_crs.to_wkt()
writer_kwargs["forward"] = "all"
writer_kwargs["offset_x"] = self.config["OFFSET_X"]
writer_kwargs["offset_y"] = self.config["OFFSET_Y"]
writer_kwargs['offset_z'] = self.config["OFFSET_Z"]
writer_kwargs["scale_x"] = self.config["SCALE_X"]
writer_kwargs["scale_y"] = self.config["SCALE_Y"]
writer_kwargs['scale_z'] = self.config["SCALE_Z"]
pipeline |= pdal.Writer.las(**writer_kwargs)

pipeline.execute()
Expand Down

0 comments on commit ca9a9a6

Please sign in to comment.