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