@@ -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 [ ] ;
@@ -763,6 +765,28 @@ function deserializeTwoFloatVectors(
763
765
return offset ;
764
766
}
765
767
768
+ function lineLength (
769
+ line : Line ,
770
+ scales : Float64Array ,
771
+ units : readonly string [ ] ,
772
+ ) {
773
+ const rank = scales . length ;
774
+ if ( rank !== line . pointA . length || rank !== line . pointB . length ) {
775
+ return ;
776
+ }
777
+ let lengthSquared = 0 ;
778
+ for ( let dim = 0 ; dim < rank ; dim ++ ) {
779
+ const unitInfo = ALLOWED_UNITS . find ( ( x ) => x . unit === units [ dim ] ) ;
780
+ if ( ! unitInfo ) {
781
+ return ;
782
+ }
783
+ const voxelToNanometers = scales [ dim ] * unitInfo . lengthInNanometers ;
784
+ lengthSquared +=
785
+ ( ( line . pointA [ dim ] - line . pointB [ dim ] ) * voxelToNanometers ) ** 2 ;
786
+ }
787
+ return Math . sqrt ( lengthSquared ) ;
788
+ }
789
+
766
790
export const annotationTypeHandlers : Record <
767
791
AnnotationType ,
768
792
AnnotationTypeHandler
@@ -826,29 +850,22 @@ export const annotationTypeHandlers: Record<
826
850
callback ( annotation . pointA , false ) ;
827
851
callback ( annotation . pointB , false ) ;
828
852
} ,
829
- defaultProperties ( annotation : Line , scales : Float64Array ) {
853
+ defaultProperties (
854
+ annotation : Line ,
855
+ scales : Float64Array ,
856
+ units : readonly string [ ] ,
857
+ ) {
830
858
const properties : AnnotationNumericPropertySpec [ ] = [ ] ;
831
859
const values : number [ ] = [ ] ;
832
- const rank = scales . length ;
833
- if (
834
- rank === annotation . pointA . length &&
835
- rank === annotation . pointB . length
836
- ) {
860
+ const length = lineLength ( annotation , scales , units ) ;
861
+ if ( length ) {
837
862
properties . push ( {
838
863
type : "float32" ,
839
864
identifier : "Length" ,
840
865
default : 0 ,
841
866
description : "Length of the line annotation in nanometers" ,
842
867
format : formatLength ,
843
868
} ) ;
844
- let length = 0 ;
845
- for ( let dim = 0 ; dim < rank ; dim ++ ) {
846
- length +=
847
- ( ( ( annotation . pointA [ dim ] - annotation . pointB [ dim ] ) / 1e-9 ) *
848
- scales [ dim ] ) **
849
- 2 ;
850
- }
851
- length = Math . sqrt ( length ) ;
852
869
values . push ( length ) ;
853
870
}
854
871
return { properties, values } ;
@@ -897,9 +914,14 @@ export const annotationTypeHandlers: Record<
897
914
visitGeometry ( annotation : Point , callback ) {
898
915
callback ( annotation . point , false ) ;
899
916
} ,
900
- defaultProperties ( annotation : Point , scales : Float64Array ) {
917
+ defaultProperties (
918
+ annotation : Point ,
919
+ scales : Float64Array ,
920
+ units : string [ ] ,
921
+ ) {
901
922
annotation ;
902
923
scales ;
924
+ units ;
903
925
return { properties : [ ] , values : [ ] } ;
904
926
} ,
905
927
} ,
@@ -973,9 +995,11 @@ export const annotationTypeHandlers: Record<
973
995
defaultProperties (
974
996
annotation : AxisAlignedBoundingBox ,
975
997
scales : Float64Array ,
998
+ units : string [ ] ,
976
999
) {
977
1000
annotation ;
978
1001
scales ;
1002
+ units ;
979
1003
return { properties : [ ] , values : [ ] } ;
980
1004
} ,
981
1005
} ,
@@ -1046,9 +1070,14 @@ export const annotationTypeHandlers: Record<
1046
1070
callback ( annotation . center , false ) ;
1047
1071
callback ( annotation . radii , true ) ;
1048
1072
} ,
1049
- defaultProperties ( annotation : Ellipsoid , scales : Float64Array ) {
1073
+ defaultProperties (
1074
+ annotation : Ellipsoid ,
1075
+ scales : Float64Array ,
1076
+ units : string [ ] ,
1077
+ ) {
1050
1078
annotation ;
1051
1079
scales ;
1080
+ units ;
1052
1081
return { properties : [ ] , values : [ ] } ;
1053
1082
} ,
1054
1083
} ,
0 commit comments