Skip to content

Commit

Permalink
Merge branch 'master' into gltf-reader
Browse files Browse the repository at this point in the history
  • Loading branch information
floryst authored Nov 11, 2024
2 parents 4073c2e + 1ca5ba1 commit 78a24e7
Show file tree
Hide file tree
Showing 17 changed files with 6,989 additions and 3,125 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 6 additions & 4 deletions Documentation/content/examples/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ This will allow you to see the some live code running in your browser. Just pick

[![ImplicitBoolean Example][ImplicitBoolean]](./ImplicitBoolean.html "ImplicitBoolean")

<div>
</div>

[ImplicitBoolean]: ../docs/gallery/ImplicitBoolean.jpg

Expand All @@ -83,7 +83,7 @@ This will allow you to see the some live code running in your browser. Just pick
[![MultiSliceImageMapper Example][MultiSliceImageMappergif]](./MultiSliceImageMapper.html "MultiSliceImageMapper")
[![TestVolumeTypes Example][TestVolumeTypes]](./TestVolumeTypes.html "TestVolumeTypes")

<div>
</div>

[MultiSliceImageMappergif]: ../docs/gallery/MultiSliceImageMapper.gif
[TestVolumeTypes]: ../docs/gallery/TestVolumeTypes.jpg
Expand All @@ -108,7 +108,7 @@ This will allow you to see the some live code running in your browser. Just pick
[![Cutter Example][Cutter]](./Cutter.html "Cutter")
[![PolyDataNormals Example][PolyDataNormals]](./PolyDataNormals.html "PolyDataNormals")

<div>
</div>

[Calculator2]: ../docs/gallery/Calculator2.jpg
[ClipClosedSurface]: ../docs/gallery/ClipClosedSurface.jpg
Expand Down Expand Up @@ -187,6 +187,7 @@ This will allow you to see the some live code running in your browser. Just pick
[![OfflineLocalView Example][OfflineLocalViewWithIcon]](./OfflineLocalView.html "Load a serialized scene (VTKSZ)")
[![G-Code Example][GCodeReaderWithIcon]](./GCodeReader.html "G-Code reader(gcode)")
[![HDRReader Example][HDRReaderWithIcon]](./HDRReader.html "Load an HDR image")
[![TGAReader Example][TGAReaderWithIcon]](./TGAReader.html "Load an TGA image")

</div>

Expand All @@ -209,6 +210,7 @@ This will allow you to see the some live code running in your browser. Just pick
[OfflineLocalViewWithIcon]: ../docs/gallery/OfflineLocalViewWithIcon.jpg
[GCodeReaderWithIcon]: ../docs/gallery/GCodeReaderWithIcon.jpg
[HDRReaderWithIcon]: ../docs/gallery/HDRReaderWithIcon.jpg
[TGAReaderWithIcon]: ../docs/gallery/TGAReaderWithIcon.jpg

# Actors

Expand Down Expand Up @@ -274,7 +276,7 @@ This will allow you to see the some live code running in your browser. Just pick
[![ImageCPRMapper Example][ImageCPRMapper]](./ImageCPRMapper.html "Curved Planar Reformat GPU mapper, stretched and straightened")
[![VolumeOutline Example][VolumeOutline]](./VolumeOutline.html "VolumeOutline")

<div>
</div>

[ImageLabelOutline]: ../docs/gallery/ImageLabelOutline.jpg
[ImageCPRMapper]: ../docs/gallery/ImageCPRMapper.jpg
Expand Down
27 changes: 27 additions & 0 deletions Sources/IO/Image/TGAReader/Constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const TYPE_INDEXED = 1;
const TYPE_RGB = 2;
const TYPE_GREY = 3;
const TYPE_RLE_INDEXED = 9;
const TYPE_RLE_RGB = 10;
const TYPE_RLE_GREY = 11;
const ORIGIN_MASK = 0x30;
const ORIGIN_SHIFT = 0x04;
const ORIGIN_BL = 0x00;
const ORIGIN_BR = 0x01;
const ORIGIN_UL = 0x02;
const ORIGIN_UR = 0x03;

export default {
TYPE_INDEXED,
TYPE_RGB,
TYPE_GREY,
TYPE_RLE_INDEXED,
TYPE_RLE_RGB,
TYPE_RLE_GREY,
ORIGIN_MASK,
ORIGIN_SHIFT,
ORIGIN_BL,
ORIGIN_BR,
ORIGIN_UL,
ORIGIN_UR,
};
122 changes: 122 additions & 0 deletions Sources/IO/Image/TGAReader/example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import '@kitware/vtk.js/favicon';

// Load the rendering pieces we want to use (for both WebGL and WebGPU)
import '@kitware/vtk.js/Rendering/Profiles/Geometry';

import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow';
import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper';
import vtkPlaneSource from '@kitware/vtk.js/Filters/Sources/PlaneSource';
import vtkTGAReader from '@kitware/vtk.js/IO/Image/TGAReader';
import vtkTexture from '@kitware/vtk.js/Rendering/Core/Texture';
import vtkURLExtract from '@kitware/vtk.js/Common/Core/URLExtract';

// ----------------------------------------------------------------------------
// Example code
// ----------------------------------------------------------------------------
const userParams = vtkURLExtract.extractURLParameters();

const reader = vtkTGAReader.newInstance();
const texture = vtkTexture.newInstance();
const planeSource = vtkPlaneSource.newInstance();
const mapper = vtkMapper.newInstance();
const actor = vtkActor.newInstance();
mapper.setInputConnection(planeSource.getOutputPort());
actor.setMapper(mapper);

// ----------------------------------------------------------------------------
// Use a file reader to load a local file
// ----------------------------------------------------------------------------

const myContainer = document.querySelector('body');
const fileContainer = document.createElement('div');
fileContainer.innerHTML =
'<div>Select a tga file.<br/><input type="file" class="file"/></div>';
myContainer.appendChild(fileContainer);

const fileInput = fileContainer.querySelector('input');

function zoomCameraToFitPlane(camera, planeWidth, planeHeight) {
const fov = 60; // Field of view in degrees

// Calculate the distance needed to fit the plane in view
const distance =
Math.max(planeWidth, planeHeight) /
(2 * Math.tan((fov * Math.PI) / 180 / 2));

// Set camera position
camera.setPosition(planeWidth / 2, planeHeight / 2, distance);
camera.setFocalPoint(planeWidth / 2, planeHeight / 2, 0);
camera.setViewUp(0, 1, 0);

// Set parallel scale for orthographic projection
camera.setParallelScale(planeHeight / 2);
}

function update() {
// Get the vtkImageData from the reader
const imageData = reader.getOutputData(0);

// Set the vtkImageData as the texture input
texture.setInputData(imageData);

// // Get the image's extent and spacing
const [xMin, xMax, yMin, yMax] = imageData.getExtent();
const [spacingX, spacingY] = imageData.getSpacing();

// // Calculate the plane's width and height based on the image's dimensions
const planeWidth = (xMax - xMin + 1) * spacingX;
const planeHeight = (yMax - yMin + 1) * spacingY;

// Set the plane's origin and corners based on calculated width and height
planeSource.setOrigin(0, 0, 0);
planeSource.setPoint1(planeWidth, 0, 0); // Horizontal edge
planeSource.setPoint2(0, planeHeight, 0); // Vertical edge

actor.addTexture(texture);

const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance();
const renderer = fullScreenRenderer.getRenderer();
const renderWindow = fullScreenRenderer.getRenderWindow();
const camera = renderer.getActiveCamera();
const interactor = renderWindow.getInteractor();

// Disable default interactor style
interactor.setInteractorStyle(null);

renderer.addActor(actor);

// Adjust the camera to fit the plane in the view
zoomCameraToFitPlane(camera, planeWidth, planeHeight);
renderer.resetCameraClippingRange();

renderWindow.render();
}

function handleFile(event) {
event.preventDefault();
const dataTransfer = event.dataTransfer;
const files = event.target.files || dataTransfer.files;
if (files.length === 1) {
const file = files[0];
const fileReader = new FileReader();
fileReader.onload = () => {
reader.parse(fileReader.result);
update();
};
fileReader.readAsArrayBuffer(file);
}
}

fileInput.addEventListener('change', handleFile);

// ----------------------------------------------------------------------------
// Use the reader to download a file
// ----------------------------------------------------------------------------
if (userParams.fileURL) {
reader.setUrl(userParams.fileURL).then(() => {
reader.loadData().then(() => {
update();
});
});
}
121 changes: 121 additions & 0 deletions Sources/IO/Image/TGAReader/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { vtkAlgorithm, vtkObject } from '../../../interfaces';
import HtmlDataAccessHelper from '../../Core/DataAccessHelper/HtmlDataAccessHelper';
import HttpDataAccessHelper from '../../Core/DataAccessHelper/HttpDataAccessHelper';
import JSZipDataAccessHelper from '../../Core/DataAccessHelper/JSZipDataAccessHelper';
import LiteHttpDataAccessHelper from '../../Core/DataAccessHelper/LiteHttpDataAccessHelper';

interface ITGAReaderOptions {
compression?: string;
progressCallback?: any;
}

/**
*
*/
export interface ITGAReaderInitialValues {}

type vtkTGAReaderBase = vtkObject &
Omit<
vtkAlgorithm,
| 'getInputData'
| 'setInputData'
| 'setInputConnection'
| 'getInputConnection'
| 'addInputConnection'
| 'addInputData'
>;

export interface vtkTGAReader extends vtkTGAReaderBase {
/**
* Get the base url.
*/
getBaseURL(): string;

/**
* Get the dataAccess helper.
*/
getDataAccessHelper():
| HtmlDataAccessHelper
| HttpDataAccessHelper
| JSZipDataAccessHelper
| LiteHttpDataAccessHelper;

/**
* Get the url of the object to load.
*/
getUrl(): string;

/**
* Load the object data.
* @param {ITGAReaderOptions} [options]
*/
loadData(options?: ITGAReaderOptions): Promise<any>;

/**
* Parse data.
* @param {ArrayBuffer} content The content to parse.
*/
parse(content: ArrayBuffer): void;

/**
* Parse data as ArrayBuffer.
* @param {ArrayBuffer} content The content to parse.
*/
parseAsArrayBuffer(content: ArrayBuffer): void;

/**
*
* @param inData
* @param outData
*/
requestData(inData: any, outData: any): void;

/**
*
* @param dataAccessHelper
*/
setDataAccessHelper(
dataAccessHelper:
| HtmlDataAccessHelper
| HttpDataAccessHelper
| JSZipDataAccessHelper
| LiteHttpDataAccessHelper
): boolean;

/**
* Set the url of the object to load.
* @param {String} url the url of the object to load.
* @param {ITGAReaderOptions} [option] The PLY reader options.
*/
setUrl(url: string, option?: ITGAReaderOptions): Promise<string | any>;
}

/**
* Method used to decorate a given object (publicAPI+model) with vtkTGAReader characteristics.
*
* @param publicAPI object on which methods will be bounds (public)
* @param model object on which data structure will be bounds (protected)
* @param {ITGAReaderInitialValues} [initialValues] (default: {})
*/
export function extend(
publicAPI: object,
model: object,
initialValues?: ITGAReaderInitialValues
): void;

/**
* Method used to create a new instance of vtkTGAReader
* @param {ITGAReaderInitialValues} [initialValues] for pre-setting some of its content
*/
export function newInstance(
initialValues?: ITGAReaderInitialValues
): vtkTGAReader;

/**
* vtkTGAReader is a source object that reads Truevision Targa(TGA) files.
*/
export declare const vtkTGAReader: {
newInstance: typeof newInstance;
extend: typeof extend;
};
export default vtkTGAReader;
Loading

0 comments on commit 78a24e7

Please sign in to comment.