@@ -31,7 +31,11 @@ import { toStringXY, rotate } from 'ol/coordinate';
3131
3232import { formatImageMetadata , getFrameMapping } from './metadata.js' ;
3333import { ROI } from './roi.js' ;
34- import { generateUID } from './utils.js' ;
34+ import {
35+ generateUID ,
36+ mapPixelCoord2SlideCoord ,
37+ mapSlideCoord2PixelCoord
38+ } from './utils.js' ;
3539import {
3640 Point ,
3741 Multipoint ,
@@ -77,6 +81,7 @@ function _getRotation(metadata) {
7781 * slide coordinate system (x, y, z), i.e. it express in which direction one
7882 * is moving in the slide coordinate system when the ROW index changes.
7983 */
84+
8085 var degrees ;
8186 if (
8287 ( metadata . ImageOrientationSlide [ 0 ] === 0 ) &&
@@ -149,7 +154,7 @@ function _getRotation(metadata) {
149154
150155
151156function _geometry2Scoord3d ( geometry , pyramid ) {
152- console . info ( 'map coordinates from image to slide coordinate system' )
157+ console . info ( 'map coordinates from pixel matrix to slide coordinate system' )
153158 const frameOfReferenceUID = pyramid [ pyramid . length - 1 ] . FrameOfReferenceUID ;
154159 const type = geometry . getType ( ) ;
155160 if ( type === 'Point' ) {
@@ -207,7 +212,7 @@ function _geometry2Scoord3d(geometry, pyramid) {
207212}
208213
209214function _scoord3d2Geometry ( scoord3d , pyramid ) {
210- console . info ( 'map coordinates from slide to image coordinate system' )
215+ console . info ( 'map coordinates from slide coordinate system to pixel matrix ' )
211216 const type = scoord3d . graphicType ;
212217 const data = scoord3d . graphicData ;
213218
@@ -269,24 +274,28 @@ function _scoord3dCoordinates2geometryCoordinates(coordinates, pyramid) {
269274*/
270275function _coordinateFormatGeometry2Scoord3d ( coordinates , pyramid ) {
271276 let transform = false ;
272- if ( ! ( coordinates [ 0 ] instanceof Array ) ) {
277+ if ( ! Array . isArray ( coordinates [ 0 ] ) ) {
273278 coordinates = [ coordinates ] ;
274279 transform = true ;
275280 }
276281 const metadata = pyramid [ pyramid . length - 1 ] ;
277282 const origin = metadata . TotalPixelMatrixOriginSequence [ 0 ] ;
278- const xOffset = Number ( origin . XOffsetInSlideCoordinateSystem ) ;
279- const yOffset = Number ( origin . YOffsetInSlideCoordinateSystem ) ;
280- const pixelSpacing = _getPixelSpacing ( metadata ) ;
281-
282- const rotation = _getRotation ( metadata ) ;
283+ const orientation = metadata . ImageOrientationSlide ;
284+ const spacing = _getPixelSpacing ( metadata ) ;
285+ const offset = [
286+ Number ( origin . XOffsetInSlideCoordinateSystem ) ,
287+ Number ( origin . YOffsetInSlideCoordinateSystem ) ,
288+ ] ;
283289
284290 coordinates = coordinates . map ( c => {
285- const position = rotate ( c , rotation ) ;
286- const x = Number ( ( xOffset - ( ( position [ 1 ] - 1 ) * pixelSpacing [ 1 ] ) ) . toFixed ( 4 ) ) ;
287- const y = Number ( ( yOffset + ( position [ 0 ] * pixelSpacing [ 0 ] ) ) . toFixed ( 4 ) ) ;
288- const z = Number ( ( 0 ) . toFixed ( 4 ) ) ;
289- return [ x , y , z ] ;
291+ const pixelCoord = [ c [ 0 ] , - ( c [ 1 ] + 1 ) ] ;
292+ const slideCoord = mapPixelCoord2SlideCoord ( {
293+ orientation,
294+ spacing,
295+ offset,
296+ point : pixelCoord
297+ } ) ;
298+ return [ slideCoord [ 0 ] , slideCoord [ 1 ] , 0 ] ;
290299 } ) ;
291300 if ( transform ) {
292301 return coordinates [ 0 ] ;
@@ -300,23 +309,28 @@ function _coordinateFormatGeometry2Scoord3d(coordinates, pyramid) {
300309*/
301310function _coordinateFormatScoord3d2Geometry ( coordinates , pyramid ) {
302311 let transform = false ;
303- if ( ! ( coordinates [ 0 ] instanceof Array ) ) {
312+ if ( ! Array . isArray ( coordinates [ 0 ] ) ) {
304313 coordinates = [ coordinates ] ;
305314 transform = true ;
306315 }
307316 const metadata = pyramid [ pyramid . length - 1 ] ;
317+ const orientation = metadata . ImageOrientationSlide ;
318+ const spacing = _getPixelSpacing ( metadata ) ;
308319 const origin = metadata . TotalPixelMatrixOriginSequence [ 0 ] ;
309- const xOffset = Number ( origin . XOffsetInSlideCoordinateSystem ) ;
310- const yOffset = Number ( origin . YOffsetInSlideCoordinateSystem ) ;
311- const pixelSpacing = _getPixelSpacing ( metadata ) ;
320+ const offset = [
321+ Number ( origin . XOffsetInSlideCoordinateSystem ) ,
322+ Number ( origin . YOffsetInSlideCoordinateSystem ) ,
323+ ] ;
312324
313- let rotation = _getRotation ( metadata ) ;
314- rotation = ( 360 - rotation * ( 180 / Math . PI ) ) * ( Math . PI / 180 )
315325 coordinates = coordinates . map ( c => {
316- const x = ( c [ 1 ] - yOffset ) / pixelSpacing [ 0 ] ;
317- const y = - ( ( c [ 0 ] - xOffset ) / pixelSpacing [ 1 ] - 1 ) ;
318- const z = c [ 2 ] ;
319- return rotate ( [ x , y , z ] , rotation ) ;
326+ const slideCoord = [ c [ 0 ] , c [ 1 ] ] ;
327+ const pixelCoord = mapSlideCoord2PixelCoord ( {
328+ offset,
329+ orientation,
330+ spacing,
331+ point : slideCoord
332+ } ) ;
333+ return [ pixelCoord [ 0 ] , - ( pixelCoord [ 1 ] + 1 ) , 0 ] ;
320334 } ) ;
321335 if ( transform ) {
322336 return coordinates [ 0 ] ;
0 commit comments