Skip to content

Commit

Permalink
Use millimeters as the default unit for diameter
Browse files Browse the repository at this point in the history
  • Loading branch information
tordanik committed Dec 13, 2024
1 parent 709d587 commit 4c6ca89
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/main/java/org/osm2world/core/util/ValueParseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,20 @@ public static final double parseSpeed(@Nullable String value, double defaultValu

/**
* parses a measure value given e.g. for the "width" or "length" key.
* This method is used if the implied default unit is not meters.
*
* @param unitlessFactor factor for values without a unit
* @return measure in m; null if value is null or has syntax errors.
*/
public static @Nullable Double parseMeasure(@Nullable String value) {
public static @Nullable Double parseMeasureWithSpecialDefaultUnit(@Nullable String value, double unitlessFactor) {

if (value == null) return null;

/* try numeric measure (implied m) */
/* try numeric measure (without unit) */

Double measure = parseOsmDecimal(value, POSITIVE);
if (measure != null) {
return measure;
return measure * unitlessFactor;
}

/* try m measure */
Expand Down Expand Up @@ -263,6 +265,16 @@ public static final double parseSpeed(@Nullable String value, double defaultValu
return null;
}

/**
* parses a measure value given e.g. for the "width" or "length" key.
* Assumes that values without units are in meters.
*
* @return measure in m; null if value is null or has syntax errors.
*/
public static @Nullable Double parseMeasure(@Nullable String value) {
return parseMeasureWithSpecialDefaultUnit(value, 1.0);
}

/** variant of {@link #parseMeasure(String)} with a default value */
public static final double parseMeasure(@Nullable String value, double defaultValue) {
Double result = parseMeasure(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.osm2world.core.target.common.material.Materials.TREE_CROWN;
import static org.osm2world.core.target.common.material.Materials.TREE_TRUNK;
import static org.osm2world.core.util.ValueParseUtil.parseMeasure;
import static org.osm2world.core.util.ValueParseUtil.parseMeasureWithSpecialDefaultUnit;
import static org.osm2world.core.world.modules.common.WorldModuleGeometryUtil.filterWorldObjectCollisions;

import java.util.*;
Expand Down Expand Up @@ -144,7 +145,7 @@ public static TreeDimensions fromTags(TagSet tags, @Nullable Random random, @Nul

double scaleFactor = random != null ? 0.5 + 0.75 * random.nextDouble() : 1.0;

Double trunkDiameter = parseMeasure(tags.getValue("diameter"));
Double trunkDiameter = parseMeasureWithSpecialDefaultUnit(tags.getValue("diameter"), 1e-3);

if (trunkDiameter == null) {
Double trunkCircumference = parseMeasure(tags.getValue("circumference"));
Expand Down

0 comments on commit 4c6ca89

Please sign in to comment.