All Downloads are FREE. Search and download functionalities are using the official Maven repository.

one.empty3.feature.DBScan Maven / Gradle / Ivy

Go to download

3D rendering engine. Plus modelling. Expected glsl textures 3d and 2d rendering3D primitives, and a lot of scenes' samples to test.+ Game Jogl reworked, Calculator (numbers and vectors). Java code parser implementation starts (<=1.2)

The newest version!
/*
 *
 *  * Copyright (c) 2024. Manuel Daniel Dahmen
 *  *
 *  *
 *  *    Copyright 2024 Manuel Daniel Dahmen
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 *
 *
 */

package one.empty3.feature;

import one.empty3.feature.app.replace.java.awt.Color;
import one.empty3.feature.app.replace.javax.imageio.ImageIO;
import one.empty3.feature.kmeans.MakeDataset;
import one.empty3.feature.kmeans.ReadDataset;
import one.empty3.io.ProcessFile;
import one.empty3.library.core.lighting.Colors;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

public class DBScan extends ProcessFile implements Algorithm {

    public List points;
    private List clusters;

    public double max_distance;
    public int min_points;

    public boolean[] visited;
    private int minPoints;

    public DBScan() {
        init(3, 3);
    }

    public void init(double max_distance, int min_points) {
        this.points = new ArrayList();
        this.clusters = new ArrayList();
        this.max_distance = max_distance;
        this.min_points = min_points;
    }

    public void cluster() {
        Iterator it = points.iterator();
        int n = 0;

        while (it.hasNext()) {

            if (!visited[n]) {
                DataPoint d = it.next();
                visited[n] = true;
                List neighbors = getNeighbors(d);

                if (neighbors.size() >= min_points) {
                    Cluster c = new Cluster(clusters.size());
                    buildCluster(d, c, neighbors);
                    clusters.add(c);
                }
            }
        }
    }

    private void buildCluster(DataPoint d, Cluster c, List neighbors) {
        c.addPoint(d);

        for (int i = 0; i < neighbors.size(); i++) {
            Integer point = neighbors.get(i);
            DataPoint p = points.get(point);
            if (!visited[point]) {
                visited[point] = true;
                List newNeighbors = getNeighbors(p);
                if (newNeighbors.size() >= min_points) {
                    neighbors.addAll(newNeighbors);
                }
            }
            if (p.getCluster() == -1) {
                c.addPoint(p);
            }
        }
    }

    private synchronized List getNeighbors(DataPoint d) {
        List neighbors = new ArrayList();
        int i = 0;
        for (DataPoint point : points) {
            double distance = d.distance(point);

            if (distance >= max_distance) {
                neighbors.add(i);
            }
            i++;
        }

        return neighbors;
    }

    public void setPoints(List points) {
        this.points = points;
        this.visited = new boolean[points.size()];
    }

    @Override
    public boolean process(File in, File out) {
        try {
            String s = in.getAbsoluteFile() + ".csv";
            MakeDataset makeDataset = new MakeDataset(in, new File(s), maxRes);

            PixM p = PixM.getPixM(Objects.requireNonNull(ImageIO.read(in)), maxRes);
            init(maxRes, minPoints);
            ReadDataset readDataset = new ReadDataset();
            readDataset.read(new File(s));
            points = list(readDataset.getFeatures());
            setPoints(points);

            cluster();

            for (Cluster cluster : clusters) {
                Color colorCluster = (Color) Colors.random();
                for (int i = 0; i < cluster.getPoints().size(); i++) {
                    DataPoint centroid = cluster.getPoints().get(i);
                    p.setValues(centroid.getX(), centroid.getY(), colorCluster.getRed(),
                            colorCluster.getGreen(), colorCluster.getBlue());
                }
            }
            ImageIO.write(p.getImage(), "jpg", out);
        } catch (Exception ex) {}

        
        return true;
    }

    private List list(List features) {
        for (double[] d : features) {
            points.add(new DbsPoint(d));
        }
        return points;
    }

    public List getPoints() {
        return points;
    }

    public List getClusters() {
        return clusters;
    }

    public void setClusters(List clusters) {
        this.clusters = clusters;
    }

    public double getMax_distance() {
        return max_distance;
    }

    public void setMax_distance(double max_distance) {
        this.max_distance = max_distance;
    }

    public int getMin_points() {
        return min_points;
    }

    public void setMin_points(int min_points) {
        this.min_points = min_points;
    }

    public boolean[] getVisited() {
        return visited;
    }

    public void setVisited(boolean[] visited) {
        this.visited = visited;
    }

    public int getMinPoints() {
        return minPoints;
    }

    public void setMinPoints(int minPoints) {
        this.minPoints = minPoints;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy