@@ -56,6 +56,7 @@ import { getRandomHexString } from "#src/util/random.js";
56
56
import { NullarySignal , Signal } from "#src/util/signal.js" ;
57
57
import { formatLength } from "#src/util/spatial_units.js" ;
58
58
import { Uint64 } from "#src/util/uint64.js" ;
59
+ import { ALLOWED_UNITS } from "#src/widget/scale_bar.js" ;
59
60
60
61
export type AnnotationId = string ;
61
62
@@ -703,6 +704,7 @@ export interface AnnotationTypeHandler<T extends Annotation = Annotation> {
703
704
defaultProperties : (
704
705
annotation : T ,
705
706
scales : Float64Array ,
707
+ units : readonly string [ ] ,
706
708
) => {
707
709
properties : AnnotationNumericPropertySpec [ ] ;
708
710
values : number [ ] ;
@@ -826,30 +828,43 @@ export const annotationTypeHandlers: Record<
826
828
callback ( annotation . pointA , false ) ;
827
829
callback ( annotation . pointB , false ) ;
828
830
} ,
829
- defaultProperties ( annotation : Line , scales : Float64Array ) {
831
+ defaultProperties (
832
+ annotation : Line ,
833
+ scales : Float64Array ,
834
+ units : readonly string [ ] ,
835
+ ) {
830
836
const properties : AnnotationNumericPropertySpec [ ] = [ ] ;
831
837
const values : number [ ] = [ ] ;
832
838
const rank = scales . length ;
833
839
if (
834
840
rank === annotation . pointA . length &&
835
841
rank === annotation . pointB . length
836
842
) {
837
- properties . push ( {
838
- type : "float32" ,
839
- identifier : "Length" ,
840
- default : 0 ,
841
- description : "Length of the line annotation in nanometers" ,
842
- format : formatLength ,
843
- } ) ;
843
+ let validLength = true ;
844
844
let length = 0 ;
845
845
for ( let dim = 0 ; dim < rank ; dim ++ ) {
846
+ const unitInfo = ALLOWED_UNITS . find ( ( x ) => x . unit === units [ dim ] ) ;
847
+ if ( ! unitInfo ) {
848
+ validLength = false ;
849
+ break ;
850
+ }
851
+ const voxelToNanometers = scales [ dim ] * unitInfo . lengthInNanometers ;
846
852
length +=
847
- ( ( ( annotation . pointA [ dim ] - annotation . pointB [ dim ] ) / 1e-9 ) *
848
- scales [ dim ] ) **
853
+ ( ( annotation . pointA [ dim ] - annotation . pointB [ dim ] ) *
854
+ voxelToNanometers ) **
849
855
2 ;
850
856
}
851
- length = Math . sqrt ( length ) ;
852
- values . push ( length ) ;
857
+ if ( validLength ) {
858
+ properties . push ( {
859
+ type : "float32" ,
860
+ identifier : "Length" ,
861
+ default : 0 ,
862
+ description : "Length of the line annotation in nanometers" ,
863
+ format : formatLength ,
864
+ } ) ;
865
+ length = Math . sqrt ( length ) ;
866
+ values . push ( length ) ;
867
+ }
853
868
}
854
869
return { properties, values } ;
855
870
} ,
@@ -897,9 +912,14 @@ export const annotationTypeHandlers: Record<
897
912
visitGeometry ( annotation : Point , callback ) {
898
913
callback ( annotation . point , false ) ;
899
914
} ,
900
- defaultProperties ( annotation : Point , scales : Float64Array ) {
915
+ defaultProperties (
916
+ annotation : Point ,
917
+ scales : Float64Array ,
918
+ units : string [ ] ,
919
+ ) {
901
920
annotation ;
902
921
scales ;
922
+ units ;
903
923
return { properties : [ ] , values : [ ] } ;
904
924
} ,
905
925
} ,
@@ -973,9 +993,11 @@ export const annotationTypeHandlers: Record<
973
993
defaultProperties (
974
994
annotation : AxisAlignedBoundingBox ,
975
995
scales : Float64Array ,
996
+ units : string [ ] ,
976
997
) {
977
998
annotation ;
978
999
scales ;
1000
+ units ;
979
1001
return { properties : [ ] , values : [ ] } ;
980
1002
} ,
981
1003
} ,
@@ -1046,9 +1068,14 @@ export const annotationTypeHandlers: Record<
1046
1068
callback ( annotation . center , false ) ;
1047
1069
callback ( annotation . radii , true ) ;
1048
1070
} ,
1049
- defaultProperties ( annotation : Ellipsoid , scales : Float64Array ) {
1071
+ defaultProperties (
1072
+ annotation : Ellipsoid ,
1073
+ scales : Float64Array ,
1074
+ units : string [ ] ,
1075
+ ) {
1050
1076
annotation ;
1051
1077
scales ;
1078
+ units ;
1052
1079
return { properties : [ ] , values : [ ] } ;
1053
1080
} ,
1054
1081
} ,
0 commit comments