From 7e35aaa96a6a3e8b98fa0f1660ceb68a0be4b7d7 Mon Sep 17 00:00:00 2001 From: dymik739 Date: Thu, 27 Apr 2023 14:23:46 +0300 Subject: [PATCH 1/5] add development files for lab3 --- labs/3/Main.java | 5 +++ labs/3/build.sh | 3 ++ labs/3/renderSuite/Camera.java | 66 ++++++++++++++++++++++++++++++++++ labs/3/renderSuite/Ray.java | 40 +++++++++++++++++++++ labs/3/renderSuite/Sphere.java | 44 +++++++++++++++++++++++ labs/3/renderSuite/Util.java | 17 +++++++++ labs/3/renderSuite/Vec3.java | 40 +++++++++++++++++++++ 7 files changed, 215 insertions(+) create mode 100644 labs/3/Main.java create mode 100644 labs/3/build.sh create mode 100644 labs/3/renderSuite/Camera.java create mode 100644 labs/3/renderSuite/Ray.java create mode 100644 labs/3/renderSuite/Sphere.java create mode 100644 labs/3/renderSuite/Util.java create mode 100644 labs/3/renderSuite/Vec3.java diff --git a/labs/3/Main.java b/labs/3/Main.java new file mode 100644 index 0000000..6054a64 --- /dev/null +++ b/labs/3/Main.java @@ -0,0 +1,5 @@ +public class Main { + public static void main(String[] args) { + + } +} diff --git a/labs/3/build.sh b/labs/3/build.sh new file mode 100644 index 0000000..01e2166 --- /dev/null +++ b/labs/3/build.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +javac -d "renderSuite-classes" renderSuite/* diff --git a/labs/3/renderSuite/Camera.java b/labs/3/renderSuite/Camera.java new file mode 100644 index 0000000..7d71a00 --- /dev/null +++ b/labs/3/renderSuite/Camera.java @@ -0,0 +1,66 @@ +package renderSuite; + +import renderSuite.Vec3; +import renderSuite.Ray; + +import java.awt.image.BufferedImage; +import javax.imageio.ImageIO; + +import java.io.File; + +public class Camera { + private Vec3 p, f; + private double fov; + private int sizeX, sizeY; + + public Camera(Vec3 position, Vec3 facing, double fov, int sizeX, int sizeY) { + this.p = position; + this.f = facing; + this.fov = fov; + this.sizeX = sizeX; + this.sizeY = sizeY; + } + + public void main() { + + } + + public void renderImageTo(String filename, Surface[] surfs) { + BufferedImage bi = new BufferedImage(this.sizeX, this.sizeY, BufferedImage.TYPE_INT_RGB); + + for (int x = 0; x < this.sizeX; x++) { + for (int y = 0; y < this.sizeY; y++) { + Vec3 result = probe(x, y); + int color = ( (int)result.x ) << 16 | ( (int)result.y ) << 8 | ( (int)result.z ); + bi.setRGB(x, y, color); + } + } + + File f = new File(filename + ".png"); + ImageIO.write(bi, "PNG", f); + } + + public Vec3 probe(int x, int y) { + Vec3 result = new Vec3(0, 0, 0); + + // creating ray to use as a probing object + Ray ray = new Ray(this.p.copy(), this.f.copy().get_norm()); + + // adjust ray facing + double offsetX = ((x/this.sizeX) - 0.5) * 2; + ray.rotate('y', offsetX * this.fov); + + double offsetY = ((y/this.sizeY) - 0.5) * 2; + ray.rotate('x', offsetY * this.fov); + + // check collisions with every object on scene + for (int i = 0; i < surfs.length; i++) { + if (surfs[i].intersect(ray)) { + return surfs[i].color; + } + } + + // if no collision detected, return black pixel + return new Vec3(0, 0, 0); + } +} diff --git a/labs/3/renderSuite/Ray.java b/labs/3/renderSuite/Ray.java new file mode 100644 index 0000000..6c6118d --- /dev/null +++ b/labs/3/renderSuite/Ray.java @@ -0,0 +1,40 @@ +package renderSuite; + +import java.lang.Math; +import renderSuite.Vec3; + +public class Ray { + // position, facing + Vec3 p, f; + + public Ray(Vec3 position, Vec3 facing) { + this.p = position; + this.f = facing; + } + + public void main() {} + + public void rotate(char axis, double angle) { + if (axis == 'x') { + this.f = new Vec3(this.f.x, + this.f.y*Math.cos(angle) - this.f.z*Math.sin(angle), + this.f.y*Math.sin(angle) + this.f.z*Math.cos(angle)); + } else if (axis == 'y') { + this.f = new Vec3(this.f.x*Math.cos(angle) + this.f.z*Math.sin(angle), + this.f.y, + this.f.z*Math.cos(angle) - this.f.x*Math.sin(angle)); + } + } + + public void move(Vec3 mv) { + this.p.x += mv.x; + this.p.y += mv.y; + this.p.z += mv.z; + } + + public void step(double l) { + this.p.x += this.f.x * l; + this.p.y += this.f.y * l; + this.p.z += this.f.z * l; + } +} diff --git a/labs/3/renderSuite/Sphere.java b/labs/3/renderSuite/Sphere.java new file mode 100644 index 0000000..63ed2b6 --- /dev/null +++ b/labs/3/renderSuite/Sphere.java @@ -0,0 +1,44 @@ +package renderSuite; + +import renderSuite.Vec3; +import renderSuite.Util; +import renderSuite.Ray; + +public class Sphere { + Vec3 p, c; + double r; + + public Sphere(Vec3 position, Vec3 color, double radius) { + this.p = position; + this.c = color; + this.r = radius; + } + + public void main() { + + } + + public boolean intersect(Ray r) { + // OLD + //double dist = Util.d(ray_position, position); + //return (Util.d(ray_position, position) <= radius); + + // using RTX + Vec3 l = new Vec3(this.p.x - r.p.x, + this.p.y - r.p.y, + this.p.z - r.p.z); + + Vec3 nl = l.get_norm(); + double cosine = Util.dot(r.f, nl); + + // >90 degrees = no intersection + if (cosine < 0) { + return false; + } + + double tc = l.len() * cosine; + double d = Math.sqrt( l.len()*l.len() - tc*tc ); + + return (d < this.r); + } +} diff --git a/labs/3/renderSuite/Util.java b/labs/3/renderSuite/Util.java new file mode 100644 index 0000000..8303941 --- /dev/null +++ b/labs/3/renderSuite/Util.java @@ -0,0 +1,17 @@ +package renderSuite; + +import java.lang.Math; +import renderSuite.Vec3; + +public class Util { + // measure distance between two points + public static double d(Vec3 p1, Vec3 p2) { + return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + + (p1.y - p2.y) * (p1.y - p2.y) + + (p1.z - p2.z) * (p1.z - p2.z)); + } + + public static double dot(Vec3 v1, Vec3 v2) { + return Math.sqrt(v1.x*v2.x + v1.y*v2.y + v1.z*v2.z); + } +} diff --git a/labs/3/renderSuite/Vec3.java b/labs/3/renderSuite/Vec3.java new file mode 100644 index 0000000..a28aade --- /dev/null +++ b/labs/3/renderSuite/Vec3.java @@ -0,0 +1,40 @@ +package renderSuite; + +import java.lang.Math; + +public class Vec3 { + public double x, y, z; + + public Vec3(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; + } + + public void main(double x, double y, double z) { + this.x = x; + this.y = y; + this.z = z; + } + + public double len() { + return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z); + } + + public void normalize() { + double l = len(); + + this.x /= l; + this.y /= l; + this.z /= l; + } + + public Vec3 get_norm() { + double l = len(); + return new Vec3(this.x / l, this.y / l, this.z / l); + } + + public Vec3 copy() { + return new Vec3(this.x, this.y, this.z); + } +} From 400aaab49d44ee458ea3be87e6a79fe1e71584a9 Mon Sep 17 00:00:00 2001 From: dymik739 Date: Tue, 16 May 2023 22:16:39 +0300 Subject: [PATCH 2/5] done the base task for lab3 --- labs/3/Finder.java | 139 +++++++++++++++++++++++++++++++++++++++++++++ labs/3/Main.java | 5 -- 2 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 labs/3/Finder.java delete mode 100644 labs/3/Main.java diff --git a/labs/3/Finder.java b/labs/3/Finder.java new file mode 100644 index 0000000..10cdc9d --- /dev/null +++ b/labs/3/Finder.java @@ -0,0 +1,139 @@ +import java.util.HashSet; +import java.util.Set; +import java.util.regex.Pattern; + +public class Finder { + public static void main(String[] args) { + StringBuilder inputStringBuilder = new StringBuilder("Testing, text to make up words iiiiiii. Testing, text without specific, up words. Therefore, we don't care about arteriscs and other useless symbols, as the only reason to use it is when the code runs horrible."); + StringBuilder[] sentences = splitStringBuilder(inputStringBuilder, "[.?!] ?"); + Set firstSentenceWords = new HashSet(); + + for (StringBuilder word : splitStringBuilder(sentences[0], ",? ")) { + firstSentenceWords.add(word); + } + + long startTime = System.nanoTime(); + + for (StringBuilder sentence : subarrayStringBuilder(sentences, 1, sentences.length)) { + StringBuilder[] words = splitStringBuilder(sentence, ",? "); + + for (StringBuilder word1 : words) { + System.out.print("Searching for " + word1 + " in " + firstSentenceWords.toString() + "..."); + + Set tempWords = new HashSet<>(firstSentenceWords); + + boolean wordFound = false; + for (StringBuilder word2 : firstSentenceWords) { + if (compareStringBuilders(word1, word2)) { + System.out.println(" found!"); + wordFound = true; + tempWords.remove(word2); + break; + } + } + + if (wordFound) { + firstSentenceWords = tempWords; + } else { + System.out.println(" absent."); + } + } + } + + long endTime = System.nanoTime(); + + /* + if (firstSentenceWords.size() == 1) { + System.out.println("Found these words: " + firstSentenceWords.toString()); + + for (StringBuilder word : firstSentenceWords) { + System.out.printf("%s, ", new String(word)); + } + + + System.out.println(); + } else if (firstSentenceWords.size() > 1) { + System.out.println("Found these words: " + } else { + System.out.println("No words found!"); + } + */ + System.out.println(getFinalMessage(firstSentenceWords)); + System.out.println("Stats: search execution took " + (endTime - startTime) + "ns"); + } + + private static StringBuilder getFinalMessage(Set s) { + if (s.size() == 1) { + return new StringBuilder("Found the word '" + s.iterator().next() + "'"); + } else if (s.size() >= 1) { + return new StringBuilder("Found more than one word (" + s.toString() + "), can't decide."); + } else { + return new StringBuilder("No such word has been found!"); + } + } + + private static boolean compareStringBuilders(StringBuilder a, StringBuilder b) + { + if (a.length() == b.length()) + { + for (int i = 0; i < a.length(); i++) + { + if (a.charAt(i) != b.charAt(i)) + { + return false; + } + } + return true; + } + else + { + return false; + } + } + + // apparently, this is incredibly complicated in Java + private static String[] subarray(String[] array, int start, int end) { + String[] result = new String[end - start]; + + for (int i = start, j = 0; i < end; i++, j++) { + result[j] = array[i]; + } + + return result; + } + + // little wrapper to simplify StringBuilder usage task + private static StringBuilder[] subarrayStringBuilder(StringBuilder[] array, int start, int end) { + String[] tempArray = subarray(stringBuilderArrayToStringArray(array), start, end); + return stringArrayToStringBuilderArray(tempArray); + } + + // name describes it well enough + private static StringBuilder[] stringArrayToStringBuilderArray(String[] inputArray) { + StringBuilder[] outputArray = new StringBuilder[inputArray.length]; + + for (int i = 0; i < inputArray.length; i++) { + outputArray[i] = new StringBuilder(inputArray[i]); + } + + return outputArray; + } + + // reverse of the above + private static String[] stringBuilderArrayToStringArray(StringBuilder[] inputArray) { + String[] outputArray = new String[inputArray.length]; + + for (int i = 0; i < inputArray.length; i++) { + outputArray[i] = new String(inputArray[i]); + } + + return outputArray; + } + + // same as String split method but using StringBuilder + private static StringBuilder[] splitStringBuilder(StringBuilder input, String regexp) { + String[] tempStrings = Pattern.compile(regexp).split(input); + return stringArrayToStringBuilderArray(tempStrings); + } + +} diff --git a/labs/3/Main.java b/labs/3/Main.java deleted file mode 100644 index 6054a64..0000000 --- a/labs/3/Main.java +++ /dev/null @@ -1,5 +0,0 @@ -public class Main { - public static void main(String[] args) { - - } -} From d606f6994f3acd02030be9532ba6b39b0f59e945 Mon Sep 17 00:00:00 2001 From: dymik739 Date: Sat, 20 May 2023 10:57:37 +0300 Subject: [PATCH 3/5] add the support of sideloading texts into the lab3 code --- labs/3/Finder.java | 57 +++++++++++++++++++--------------- labs/3/lab3lib/Fetcher.java | 49 +++++++++++++++++++++++++++++ labs/3/lab3lib/fetchContent.py | 10 ++++++ 3 files changed, 91 insertions(+), 25 deletions(-) create mode 100644 labs/3/lab3lib/Fetcher.java create mode 100644 labs/3/lab3lib/fetchContent.py diff --git a/labs/3/Finder.java b/labs/3/Finder.java index 10cdc9d..432dff1 100644 --- a/labs/3/Finder.java +++ b/labs/3/Finder.java @@ -1,31 +1,41 @@ import java.util.HashSet; import java.util.Set; import java.util.regex.Pattern; +import lab3lib.Fetcher; public class Finder { public static void main(String[] args) { - StringBuilder inputStringBuilder = new StringBuilder("Testing, text to make up words iiiiiii. Testing, text without specific, up words. Therefore, we don't care about arteriscs and other useless symbols, as the only reason to use it is when the code runs horrible."); - StringBuilder[] sentences = splitStringBuilder(inputStringBuilder, "[.?!] ?"); + boolean demoContent = false; + + StringBuilder inputStringBuilder = new StringBuilder(); + if (demoContent) { + inputStringBuilder = new StringBuilder("Testing, text to make up words. Testing, text without specific, up words. Therefore, we don't care about arteriscs and other useless symbols, as the only reason to use it is when the code runs horrible."); + } else { + inputStringBuilder = receiveText(); + } + + System.out.println("Processing string: " + inputStringBuilder); + + final StringBuilder[] sentences = splitStringBuilder(inputStringBuilder, "[.?!] ?"); Set firstSentenceWords = new HashSet(); for (StringBuilder word : splitStringBuilder(sentences[0], ",? ")) { firstSentenceWords.add(word); } - long startTime = System.nanoTime(); + final long startTime = System.nanoTime(); for (StringBuilder sentence : subarrayStringBuilder(sentences, 1, sentences.length)) { StringBuilder[] words = splitStringBuilder(sentence, ",? "); for (StringBuilder word1 : words) { - System.out.print("Searching for " + word1 + " in " + firstSentenceWords.toString() + "..."); + System.out.print("Searching for '" + toLowerCaseStringBuilder(word1) + "' in " + firstSentenceWords.toString() + "..."); Set tempWords = new HashSet<>(firstSentenceWords); boolean wordFound = false; for (StringBuilder word2 : firstSentenceWords) { - if (compareStringBuilders(word1, word2)) { - System.out.println(" found!"); + if (compareStringBuilders(toLowerCaseStringBuilder(word1), toLowerCaseStringBuilder(word2))) { wordFound = true; tempWords.remove(word2); break; @@ -33,6 +43,7 @@ public class Finder { } if (wordFound) { + System.out.println(" found!"); firstSentenceWords = tempWords; } else { System.out.println(" absent."); @@ -40,33 +51,29 @@ public class Finder { } } - long endTime = System.nanoTime(); - - /* - if (firstSentenceWords.size() == 1) { - System.out.println("Found these words: " + firstSentenceWords.toString()); + final long endTime = System.nanoTime(); - for (StringBuilder word : firstSentenceWords) { - System.out.printf("%s, ", new String(word)); - } - - - System.out.println(); - } else if (firstSentenceWords.size() > 1) { - System.out.println("Found these words: " - } else { - System.out.println("No words found!"); - } - */ System.out.println(getFinalMessage(firstSentenceWords)); System.out.println("Stats: search execution took " + (endTime - startTime) + "ns"); } + private static StringBuilder receiveText() { + try { + return Fetcher.fetchTextFromPython(); + } catch (Exception e) { + return new StringBuilder("Testing, text to make up words. Testing, text without specific, up words. Therefore, we don't care about arteriscs and other useless symbols, as the only reason to use it is when the code runs horrible."); + } + } + + private static StringBuilder toLowerCaseStringBuilder(StringBuilder inputStringBuilder) { + return new StringBuilder(new String(inputStringBuilder).toLowerCase()); + } + private static StringBuilder getFinalMessage(Set s) { if (s.size() == 1) { return new StringBuilder("Found the word '" + s.iterator().next() + "'"); - } else if (s.size() >= 1) { - return new StringBuilder("Found more than one word (" + s.toString() + "), can't decide."); + } else if (s.size() > 1) { + return new StringBuilder("Found more than one word (" + s.toString() + "), can't pick one."); } else { return new StringBuilder("No such word has been found!"); } diff --git a/labs/3/lab3lib/Fetcher.java b/labs/3/lab3lib/Fetcher.java new file mode 100644 index 0000000..f554170 --- /dev/null +++ b/labs/3/lab3lib/Fetcher.java @@ -0,0 +1,49 @@ +package lab3lib; + +//import java.net.URL; +//import java.io.BufferedInputStream; +import java.util.Scanner; +import java.lang.Exception; +//import javax.json.JsonObject; + +public class Fetcher { + public void main() {} + + /* + private static String fetchString(String remote_url) { + try { + Scanner reader = new Scanner(new URL(remote_url).openStream(), "UTF-8"); + String jsonString = ""; + + if (reader.hasNextLine()) { + jsonString = reader.nextLine(); + } + + return jsonString; + } catch (Exception e) { + System.out.println("[ERROR] Failed to fetch resource from " + remote_url + " due to the following exception: " + e); + System.exit(1); + } + } + */ + + public static StringBuilder fetchTextFromPython() { + try { + Process contentFetcher = Runtime.getRuntime().exec("python3 lab3lib/fetchContent.py"); + Scanner reader = new Scanner(contentFetcher.getInputStream()); + return new StringBuilder(reader.nextLine()); + } catch (Exception e) { + return new StringBuilder(""); + } + } + + /* + public StringBuilder fetchText(String request) { + String responce = fetchString("http://10.1.1.2:8080/search?language=en-US&format=json&q=" + request); + JSONObject results = new JSONObject(responce); + return results.get("results").get(0).get("content"); + } + */ +} + +//JSONObject results = 'http://10.1.1.2:8080/search?q=test&language=en-US&format=json' diff --git a/labs/3/lab3lib/fetchContent.py b/labs/3/lab3lib/fetchContent.py new file mode 100644 index 0000000..df28032 --- /dev/null +++ b/labs/3/lab3lib/fetchContent.py @@ -0,0 +1,10 @@ +import requests +from random import randint +import json + +r = requests.get("http://10.1.1.2:8080/search?q=test&format=json&language=en-US") + +results = json.loads(r.text)["results"] +final_content = results[randint(0, len(results))]['content'] + +print(final_content) From 03bbb6e4c50208bf5468c1285f1d93df1bc6ad1b Mon Sep 17 00:00:00 2001 From: dymik739 Date: Sat, 20 May 2023 10:59:09 +0300 Subject: [PATCH 4/5] continue renderSuite toolkit development --- labs/3/renderSuite/Sphere.java | 3 +-- labs/3/renderSuite/Surface.java | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 labs/3/renderSuite/Surface.java diff --git a/labs/3/renderSuite/Sphere.java b/labs/3/renderSuite/Sphere.java index 63ed2b6..054cbfa 100644 --- a/labs/3/renderSuite/Sphere.java +++ b/labs/3/renderSuite/Sphere.java @@ -4,8 +4,7 @@ import renderSuite.Vec3; import renderSuite.Util; import renderSuite.Ray; -public class Sphere { - Vec3 p, c; +public class Sphere extends Surface { double r; public Sphere(Vec3 position, Vec3 color, double radius) { diff --git a/labs/3/renderSuite/Surface.java b/labs/3/renderSuite/Surface.java new file mode 100644 index 0000000..bd51fdd --- /dev/null +++ b/labs/3/renderSuite/Surface.java @@ -0,0 +1,39 @@ +package renderSuite; + +import renderSuite.Vec3; +//import renderSuite.Util; +//import renderSuite.Ray; + +public class Surface { + Vec3 p, c; + + public void main() { + + } + + /* + public boolean intersect(Ray r) { + // OLD + //double dist = Util.d(ray_position, position); + //return (Util.d(ray_position, position) <= radius); + + // using RTX + Vec3 l = new Vec3(this.p.x - r.p.x, + this.p.y - r.p.y, + this.p.z - r.p.z); + + Vec3 nl = l.get_norm(); + double cosine = Util.dot(r.f, nl); + + // >90 degrees = no intersection + if (cosine < 0) { + return false; + } + + double tc = l.len() * cosine; + double d = Math.sqrt( l.len()*l.len() - tc*tc ); + + return (d < this.r); + } + */ +} From 709432274467c6c1ff09b8ec50feb6339781d372 Mon Sep 17 00:00:00 2001 From: dymik739 Date: Tue, 23 May 2023 09:34:05 +0300 Subject: [PATCH 5/5] lab3: add missing files, freeze the development --- labs/3/Benchmarker.java | 13 +++++++++++++ labs/3/renderSuite/Letter.java | 7 +++++++ 2 files changed, 20 insertions(+) create mode 100644 labs/3/Benchmarker.java create mode 100644 labs/3/renderSuite/Letter.java diff --git a/labs/3/Benchmarker.java b/labs/3/Benchmarker.java new file mode 100644 index 0000000..b03d50b --- /dev/null +++ b/labs/3/Benchmarker.java @@ -0,0 +1,13 @@ +import lab3lib.Finder; + +public class Benchmarker { + public static void main(String[] args) { + System.out.print("Timing object creation..."); + long startTime = System.nanoTime(); + Finder obj = new Finder(); + long endTime = System.nanoTime(); + + System.out.println(" Finished!"); + System.out.println("Operation took " + (endTime - startTime) + "ns"); + } +} diff --git a/labs/3/renderSuite/Letter.java b/labs/3/renderSuite/Letter.java new file mode 100644 index 0000000..9dc43b2 --- /dev/null +++ b/labs/3/renderSuite/Letter.java @@ -0,0 +1,7 @@ +import +public class Letter { + this. + public void main() { + + } +}