void eventDisplayCurrentDicom(object obj = null) { DICOM3D dicom = DICOMLoader.instance.currentDICOMVolume; if (dicom != null) { SetDicom(dicom); GetComponent <MeshRenderer> ().enabled = true; } }
/*! Set a volume DICOM which is to be rendered in 3D */ public void SetDicom(DICOM3D dicom) { Material mat = GetComponent <MeshRenderer> ().sharedMaterial; Debug.Log("Min, max: " + (float)dicom.seriesInfo.minPixelValue + " " + (float)dicom.seriesInfo.maxPixelValue); mat.SetFloat("globalMinimum", (float)dicom.seriesInfo.minPixelValue); mat.SetFloat("globalMaximum", (float)dicom.seriesInfo.maxPixelValue); mat.mainTexture = dicom.getTexture3D(); mat.SetVector("textureSize", new Vector3(dicom.texWidth, dicom.texHeight, dicom.texDepth)); // ------------------------------------------------ // Move the VolumeCube to the position where the 3D DICOM should be rendered: // (The VolumeCube's localPosition and localScale is in the DICOM's "patient coordinate system", so // adjusting its localPosition and localScale to fit with the DICOM's center and size will set it to // the correct position.) // Size which the volume should have at the end (in patient coordinates): Vector3 goalSize = dicom.boundingBox.size; // Position where the center of the rendered volume should be (in patient coordinates): Vector3 goalCenter = dicom.boundingBox.center; // Current (original) position and sizes of the mesh, because of the way it was generated: Vector3 minMesh = new Vector3(-1, -1, -1); Vector3 maxMesh = new Vector3(1, 1, 1); Vector3 meshSize = maxMesh - minMesh; // The padding factor takes into account that the DICOM volume may be smaller than the texture, since // Unity requires power-of-two textures, so the texture may be larger than the DICOM volume. Vector3 paddingFactor = new Vector3( (float)dicom.origTexWidth / (float)dicom.texWidth, (float)dicom.origTexHeight / (float)dicom.texHeight, (float)dicom.origTexDepth / (float)dicom.texDepth); // Part of the mesh which actually holds the DICOM volume: Vector3 paddedMeshSize = Vector3.Scale(paddingFactor, meshSize); // Center of the part of the mesh which is filled with the DICOM volume: Vector3 meshCenter = minMesh + paddedMeshSize * 0.5f; // Offset from the mesh original center (which is, because of the way the mesh was generated above, zero): Vector3 meshCenterOffset = Vector3.zero - meshCenter; // Inverse of paddedMeshSize: Vector3 invPaddedMeshSize = new Vector3(1f / paddedMeshSize.x, 1f / paddedMeshSize.y, 1f / paddedMeshSize.z); // The final scale is the goalSize/paddedMeshSize: Vector3 scale = Vector3.Scale(goalSize, invPaddedMeshSize); transform.localScale = scale; // Move the mesh center to its final position: transform.localPosition = goalCenter + Vector3.Scale(meshCenterOffset, scale); // ------------------------------------------------ }