org.joml.Vector2i Java Examples

The following examples show how to use org.joml.Vector2i. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GridPane.java    From LWJGUI with MIT License 6 votes vote down vote up
private Node searchForNextNode( Vector2i position, Vector2i direction ) {
	Node ret = null;
	int tries = 0;
	
	int x = position.x;
	int y = position.y;
	
	while ( ret == null && tries < 32 ) {
		x += direction.x;
		y += direction.y;
		if ( x >= 0 && x < maxX && y >= 0 && y < maxY ) {
			ret = elements[x][y];
		}
		tries++;
	}
	
	return ret;
}
 
Example #2
Source File: Pin.java    From BlockMap with MIT License 6 votes vote down vote up
/**
 * Takes in a set of chunk positions and identifies all connected subsets.
 * 
 * @param chunks
 *            A set of chunk positions. This will be emptied during the calculation.
 */
private static List<Set<Vector2ic>> splitChunks(Set<Vector2ic> chunks) {
	List<Set<Vector2ic>> islands = new ArrayList<>();
	while (!chunks.isEmpty()) {
		Set<Vector2ic> done = new HashSet<>();
		Queue<Vector2ic> todo = new LinkedList<>();
		todo.add(chunks.iterator().next());
		chunks.remove(todo.element());
		while (!todo.isEmpty()) {
			Vector2ic current = todo.remove();
			for (Vector2i neighbor : new Vector2i[] { new Vector2i(-1, 0), new Vector2i(1, 0), new Vector2i(0, -1), new Vector2i(0, 1) }) {
				neighbor.add(current);
				if (chunks.remove(neighbor))
					todo.add(neighbor);
			}
			done.add(current);
		}
		islands.add(done);
	}
	return islands;
}
 
Example #3
Source File: Pin.java    From BlockMap with MIT License 6 votes vote down vote up
/**
 * Takes in a map of chunk positions and identifies all connected subsets.
 * 
 * @param chunks
 *            A map of chunk positions. This will be emptied during the calculation.
 */
private static <T> List<Map<Vector2ic, T>> splitChunks(Map<Vector2ic, T> chunks) {
	List<Map<Vector2ic, T>> islands = new ArrayList<>();
	while (!chunks.isEmpty()) {
		Map<Vector2ic, T> done = new HashMap<>();
		Queue<Vector2ic> todo = new LinkedList<>();
		Queue<T> todoV = new LinkedList<>();
		todo.add(chunks.entrySet().iterator().next().getKey());
		todoV.add(chunks.entrySet().iterator().next().getValue());
		chunks.remove(todo.element());
		while (!todo.isEmpty()) {
			Vector2ic current = todo.remove();
			T val = todoV.remove();
			for (Vector2i neighbor : new Vector2i[] { new Vector2i(-1, 0), new Vector2i(1, 0), new Vector2i(0, -1), new Vector2i(0, 1) }) {
				neighbor.add(current);
				if (chunks.containsKey(neighbor)) {
					todoV.add(chunks.remove(neighbor));
					todo.add(neighbor);
				}
			}
			done.put(current, val);
		}
		islands.add(done);
	}
	return islands;
}
 
Example #4
Source File: RegionRendererTest.java    From BlockMap with MIT License 6 votes vote down vote up
/** Test if bounds in the render settings are respected */
@Test
public void testBounds() throws IOException, URISyntaxException, InterruptedException {
	System.setProperty("joml.format", "false");
	RenderSettings settings = new RenderSettings();
	settings.loadDefaultColors();
	settings.minZ = -500;
	settings.maxZ = -50;
	settings.minX = 30;
	settings.maxX = 420;
	RegionRenderer renderer = new RegionRenderer(settings);
	BufferedImage image = renderer.render(new Vector2i(0, -1), new RegionFile(Paths.get(URI.create(getClass().getResource("/r.1.3.mca").toString()))))
			.getImage();
	boolean[][] isCulled = new boolean[512][];
	boolean[][] shouldBeCulled = new boolean[512][];
	for (int x = 0; x < 512; x++) {
		isCulled[x] = new boolean[512];
		shouldBeCulled[x] = new boolean[512];
		for (int z = 0; z < 512; z++) {
			isCulled[x][z] = (image.getRGB(x, z) >> 24) == 0;
			shouldBeCulled[x][z] = z < 12 || z > 462 || x < 30 || x > 420;
		}
	}
	assertArrayEquals(shouldBeCulled, isCulled);
}
 
Example #5
Source File: RenderedMap.java    From BlockMap with MIT License 6 votes vote down vote up
public RenderedMap(RegionFolder regionFolder, ExecutorService executor, ReadOnlyObjectProperty<Vector2dc> mouseWorldProperty) {
	this.regionFolder = Objects.requireNonNull(regionFolder);
	this.mouseWorldProperty = Objects.requireNonNull(mouseWorldProperty);
	Collection<Vector2ic> regions = regionFolder.listRegions();
	if (regions.isEmpty())
		throw new IllegalArgumentException("World can not be empty");
	regionFolder.listRegions().stream().map(r -> new RenderedRegion(this, r)).forEach(r -> plainRegions.put(r.position, r));

	this.regions.put(0, plainRegions);
	for (int i = 1; i <= DisplayViewport.MAX_ZOOM_LEVEL; i++) {
		int j = i;
		this.regions.put(i, this.regions.get(i - 1)
				.keySet()
				.stream()
				.map(v -> new Vector2i(v.x() >> 1, v.y() >> 1))
				.distinct()
				.collect(Collectors.toMap(Function.identity(), k -> new RenderedRegion(this, j, k))));
	}

	regionsRendered = 0;
	regionsCount = regions.size();
	notRendered.addAll(plainRegions.values());
	/* Create one task per region file. Don't specify which one yet, this will be determined later on the fly */
	for (int i = 0; i < regionsCount; i++)
		executor.submit(this);
}
 
Example #6
Source File: Generator.java    From BlockMap with MIT License 6 votes vote down vote up
@Command
public void generateTestWorld() throws IOException, InterruptedException {
	log.info("Generating test world");

	Path worldPath = OUTPUT_INTERNAL_CACHE.resolve("BlockMapWorld");

	FileUtils.copyDirectory(new File(URI.create(Generator.class.getResource("/BlockMapWorld").toString())), worldPath
			.toFile());

	Server server = new Server(OUTPUT_INTERNAL_CACHE.resolve("server-" + MinecraftVersion.LATEST.fileSuffix + ".jar"), null);
	World world = server.initWorld(worldPath, true);

	int SIZE = 256;// 256;
	ArrayList<Vector2i> chunks = new ArrayList<>(SIZE * SIZE * 4);
	for (int z = -SIZE; z < SIZE; z++)
		for (int x = -SIZE; x < SIZE; x++)
			chunks.add(new Vector2i(x, z));
	MinecraftLandGenerator.forceloadChunks(server, world, chunks, Dimension.OVERWORLD, true, 1024, true);
	world.resetChanges();

	processResources();
}
 
Example #7
Source File: RectangleiTest.java    From JOML with MIT License 5 votes vote down vote up
public void testRectangleContainsPoints() {
    Rectanglei rect = new Rectanglei(0, 0, 3, 3);

    Assert.assertTrue(rect.isValid());
    Assert.assertTrue(rect.containsPoint(new Vector2i(0, 0)));
    Assert.assertFalse(rect.containsPoint(new Vector2i(-1, -1)));
    Assert.assertFalse(rect.containsPoint(new Vector2i(4, 4)));
}
 
Example #8
Source File: RegionFolder.java    From BlockMap with MIT License 5 votes vote down vote up
/**
 * Loads a region folder from a given path. All region files found in this folder (not searching recursively) will be added to the returned
 * object. Files added later on won't be recognized. Removing files will lead to errors when trying to render them. All files whose name
 * matches {@code ^r\.(-?\d+)\.(-?\d+)\.mca$} are taken. If one of them isn't a proper region file, rendering it will fail.
 * 
 * @param regionFolder
 *            the path to the folder containing all region files. This folder is commonly called {@code region} and is situated inside a
 *            Minecraft world, but this is not a hard requirement. It has to be a directory.
 */
public static WorldRegionFolder load(Path regionFolder, RegionRenderer renderer) throws IOException {
	Map<Vector2ic, Path> files = new HashMap<>();
	try (Stream<Path> stream = Files.list(regionFolder)) {
		for (Path p : (Iterable<Path>) stream::iterator) {
			Matcher m = rfpat.matcher(p.getFileName().toString());
			if (m.matches())
				files.put(new Vector2i(Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2))), p);
		}
	}
	return new WorldRegionFolder(files, renderer);
}
 
Example #9
Source File: RegionRendererTest.java    From BlockMap with MIT License 5 votes vote down vote up
@Test
public void simpleTest1() throws IOException, URISyntaxException, InterruptedException {
	RenderSettings settings = new RenderSettings();
	settings.loadDefaultColors();
	RegionRenderer renderer = new RegionRenderer(settings);
	BufferedImage image = renderer.render(new Vector2i(0, 0), new RegionFile(Paths.get(URI.create(getClass().getResource("/r.0.0.mca").toString()))))
			.getImage();
	ImageIO.write(image, "png", Files.newOutputStream(folder.newFile().toPath()));
}
 
Example #10
Source File: RenderBenchmark.java    From BlockMap with MIT License 5 votes vote down vote up
@Benchmark
public void benchmark(Blackhole hole) throws IOException {
	RenderSettings settings = new RenderSettings();
	settings.blockColors = blockColors;
	settings.biomeColors = biomeColors;

	RegionRenderer renderer = new RegionRenderer(settings);
	hole.consume(render(renderer, new Vector2i(-1, 1)));
	hole.consume(render(renderer, new Vector2i(0, 1)));
	hole.consume(render(renderer, new Vector2i(-1, 2)));
	hole.consume(render(renderer, new Vector2i(0, 2)));
}
 
Example #11
Source File: Screenshots.java    From BlockMap with MIT License 5 votes vote down vote up
private static BufferedImage generateScreenshot(RegionRenderer renderer, RenderSettings settings, Vector2i toRender, BlockColorMap.InternalColorMap colors)
		throws IOException {
	RegionFile file = new RegionFile(Paths.get(URI.create(Generator.class.getResource("/BlockMapWorld/region/r." + toRender.x + "." + toRender.y
			+ ".mca")
			.toString())));
	settings.blockColors = colors.getColorMap();
	return renderer.render(toRender, file).getImage();
}
 
Example #12
Source File: PinDecoration.java    From BlockMap with MIT License 5 votes vote down vote up
public void reloadWorld() {
	byGroup.forEach(PinGroup::remove);
	byGroup.clear();
	byRegion.values().forEach(PinRegion::unload);
	for (Pin p : staticPins) {
		Vector2i pos = new Vector2i((int) (p.position.x()) >> 9, (int) (p.position.y()) >> 9);
		if (!byRegion.containsKey(pos))
			log.warn("Pin " + p + " is outside of the world's bounds and will be ignored");
		else
			byRegion.get(pos).pins.add(p);
	}
}
 
Example #13
Source File: PinDecoration.java    From BlockMap with MIT License 5 votes vote down vote up
public void loadWorld(Collection<Vector2ic> regions, Collection<Pin> staticPins) {
	this.staticPins = Objects.requireNonNull(staticPins);

	byRegion = regions.stream().collect(Collectors.toMap(Function.identity(), PinRegion::new));
	for (Vector2ic r : byRegion.keySet())
		byRegion.get(r).neighbors = Arrays.stream(connectivity8)
				.map(v -> v.add(r, new Vector2i()))
				.filter(byRegion::containsKey)
				.map(byRegion::get)
				.collect(Collectors.toList())
				.toArray(new PinRegion[0]);
	reloadWorld();
}
 
Example #14
Source File: RegionFolder.java    From BlockMap with MIT License 5 votes vote down vote up
/**
 * Loads a json file that contains the information about all rendered files.
 * 
 * @see #parseSaved(JsonElement)
 */
protected SavedRegionFolder(T file) throws IOException {
	this.basePath = file;
	SavedRegionHelper helper = load(file);
	pins = Optional.ofNullable(helper.pins);
	regions = Optional.ofNullable(helper.regions)
			.stream().flatMap(Collection::stream)
			.collect(Collectors.toMap(r -> new Vector2i(r.x, r.z), Function.identity()));

}
 
Example #15
Source File: RenderBenchmark.java    From BlockMap with MIT License 4 votes vote down vote up
private Region render(RegionRenderer renderer, Vector2i position) throws IOException {
	return renderer.render(position, new RegionFile(resourcePath.resolve("BlockMapWorld/region/r." + position.x + "." + position.y + ".mca")));
}
 
Example #16
Source File: BlurPane.java    From LWJGUI with MIT License 4 votes vote down vote up
@Override
public void render(Context context) {
	if ( !isVisible() )
		return;
	
	// Check for resize
	Vector2i newDims = new Vector2i((int)getWidth(),(int)getHeight());
	if ( !newDims.equals(oldSize) || nanoImage == -1 ) {
		oldSize.set(newDims);
		resizeBuffer();
	}

	// FBO Rendering
	if ( nanoImage != -1 ) {
		NanoVG.nvgSave(context.getNVG());
		NanoVG.nvgEndFrame(context.getNVG());
		
		// Blit (Copy current FBO to BlurPane FBO)
		blit(context);
		
		// Blur (Blur the FBO)
		blur(context);
		
		// Restore nanovg
		NanoVG.nvgRestore(context.getNVG());
		context.refresh(); // Restore glViewport

		// Render children (Apply Styling)
		super.render(context);

		// Render FBO to screen (Draw our blurred image ontop)
		long nanovg = context.getNVG();
		float x = (int)this.getX();
		float y = (int)this.getY();
		float w = (int)this.getWidth();
		float h = (int)this.getHeight();
		try (MemoryStack stack = stackPush()) {
			NVGPaint imagePaint = NanoVG.nvgImagePattern(nanovg, x, y, w, h, 0, nanoImage, 1, NVGPaint.callocStack(stack));
			NanoVG.nvgBeginPath(nanovg);
			NanoVG.nvgRoundedRectVarying(context.getNVG(), (int)x, (int)y, (int)w, (int)h, (float)this.getBorderRadii()[0], (float)this.getBorderRadii()[1], (float)this.getBorderRadii()[2], (float)this.getBorderRadii()[3]);
			NanoVG.nvgFillPaint(nanovg, imagePaint);
			NanoVG.nvgFill(nanovg);
		}
	}
}
 
Example #17
Source File: RenderedRegion.java    From BlockMap with MIT License 4 votes vote down vote up
public RenderedRegion(RenderedMap map, int level, Vector2ic position) {
	this.map = map;
	this.level = level;
	this.position = new Vector2i(position);
	setImage(null);
}
 
Example #18
Source File: OpenGLPane.java    From LWJGUI with MIT License 4 votes vote down vote up
@Override
public void render(Context context) {
	if ( !isVisible() )
		return;
	
	// Check for resize
	Vector2i newDims = new Vector2i((int)getWidth(),(int)getHeight());
	if ( !newDims.equals(oldSize) || nanoImage == -1 ) {
		oldSize.set(newDims);
		resizeBuffer();
	}
	
	// FBO Rendering
	if ( renderer != null && nanoImage != -1 ) {
		NanoVG.nvgSave(context.getNVG());
		//NanoVG.nvgEndFrame(context.getNVG());
		{
			// Bind & render to FBO
			this.buffer.bind();
			{
				// Background fill
				if ( this.internalBackground != null && isAutoClear() ) {
					float r = internalBackground.getRed()/255f;
					float g = internalBackground.getGreen()/255f;
					float b = internalBackground.getBlue()/255f;
					GL11.glClearColor(r, g, b, 1);
					GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
				}
				
				// Viewport
				GL11.glViewport(0, 0, (int)getWidth(), (int)getHeight());
				
				// Drawing
				NanoVG.nvgBeginFrame(internalContext.getNVG(), (int)getWidth(), (int)getHeight(), window.getPixelRatio());
				//internalContext.refresh(context);
				renderer.render(internalContext, (int)getWidth(), (int)getHeight());
				NanoVG.nvgEndFrame(internalContext.getNVG());
			}
			this.buffer.unbind();
		}
		
		// Restore nanovg
		NanoVG.nvgRestore(context.getNVG());
		context.refresh(); // Restore glViewport

		// Render FBO to screen
		long nanovg = context.getNVG();
		float x = (int)this.getX();
		float y = (int)this.getY();
		float w = (int)this.getWidth();
		float h = (int)this.getHeight();
		if ( flipY ) {
			y = y + h;
			h = -h;
		}
		try (MemoryStack stack = stackPush()) {
			NVGPaint imagePaint = NanoVG.nvgImagePattern(nanovg, x, y, w, h, 0, nanoImage, 1, NVGPaint.callocStack(stack));
			NanoVG.nvgBeginPath(nanovg);
			NanoVG.nvgRect(nanovg, x, y, w, h);
			NanoVG.nvgFillPaint(nanovg, imagePaint);
			NanoVG.nvgFill(nanovg);
		}
	}
	
	// Render children
	super.render(context);
}