; docformat = 'rst' ; ; NAME: ; facetNormals ; PURPOSE: ; Compute the normal vectors to each facet of a shape model ;+ ; :Description: ; Compute the normal vectors to each facet of a shape model ; ; :Categories: ; Shape Models ; ; :Params: ; V: in, required, type=fltarr or structure ; The cartesian coordinates of the shape vertices, or a model ; structure as provided by readVer ; P: in, optional, type=lonarr() ; Connectivity array ([N_vertex, ID_vertex....]), if V are coordinates ; ; :Returns: The normal vectors to all facets ; ; :Examples: ; ; :Uses: ; ; :Author: ; B.Carry (IMCCE) ; ; :History: ; Change History:: ; Original Version written in 2006, J. Berthier (IMCCE) ; 2013 Nov. - B.Carry (IMCCE) - Function cleaned ; 2014 Apr. - B.Carry (IMCCE) - Header compatible with idlDoc ; - Checks on I/O added ;- function facetNormals, V, P COMPILE_OPT hidden ;--I-- Input Verification ----------------------------------------------------------- if N_params() LT 1 then begin message, /IOERROR, 'Syntax - normals = facetNormals(V, P)' return, -1 endif ;--II-- Interpret V as Coordinates or Structure ------------------------------------ dimV=size(V) ;--II.1-- V = Structure if dimV(dimv(0)+1) eq 8 then begin model=V V=model.vertex.xyz P=model.facet.con nbFacet=model.facet.N ;--II.2-- V = Coordinates endif else begin ;--II.2.1-- Connectivity Required if not keyword_set(P) then begin message, /IOERROR, 'Syntax - normals = facetNormals(V, P)' return, -1 endif nbFacet = (n_elements(P))/4. endelse ;--III-- Compute Facet Normals ------------------------------------------------------ N = fltarr(3,nbFacet) for i=0L, nbFacet-1L do begin ;--III.1-- Cross-product: side1 to side2 cross = crossp( V(*,P(4*i+2))-V(*,P(4*i+1)), V(*,P(4*i+3))-V(*,P(4*i+1)) ) ;--III.2-- Normalization N(*,i) = cross/sqrt(total( cross^2 )) endfor ;--IV-- Return Normals -------------------------------------------------------- return, N end