1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) 2018-2020 Rafael Villar Burke <pachi@ietcc.csic.es>
// Distributed under the MIT License
// (See acoompanying LICENSE file or a copy at http://opensource.org/licenses/MIT)

// Utilidades varias

use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::path::{Path, PathBuf};

use anyhow::{bail, Context, Error};
use encoding::all::ISO_8859_1;
use encoding::{DecoderTrap, Encoding};

use glob::glob;

/// Localiza archivo que sigue el patrón pat en el directorio dir
/// Falla si hay algún error en el patrón
pub fn find_file_in_basedir<T: AsRef<str>>(dir: T, pat: &str) -> Result<Option<PathBuf>, Error> {
    let dir = dir.as_ref();
    if !PathBuf::from(dir).exists() {
        bail!("No se ha localizado el directorio {}", dir);
    }

    let pattern = [dir, pat]
        .iter()
        .collect::<PathBuf>()
        .to_string_lossy()
        .into_owned();

    let globiter = glob(&pattern)?;
    match globiter.map(|r| r).next() {
        Some(p) => Ok(Some(p?)),
        None => Ok(None),
    }
}

// Busca el primer archivo que coincida con el patrón dado
pub fn find_first_file(pattern: &str) -> Result<Option<PathBuf>, Error> {
    let globiter = glob(pattern)?;
    match globiter.map(|r| r).next() {
        Some(p) => Ok(Some(p?)),
        None => Ok(None),
    }
}

// Lee a una cadena un archivo en latin1
pub fn read_latin1_file<T: AsRef<Path>>(path: T) -> Result<String, Error> {
    let buf = {
        let mut buf = Vec::new();
        BufReader::new(File::open(path.as_ref())?)
            .read_to_end(&mut buf)
            .with_context(|| {
                format!(
                    "No se ha podido leer el archivo {}",
                    path.as_ref().display()
                )
            })?;
        buf
    };

    match ISO_8859_1.decode(&buf, DecoderTrap::Replace) {
        Ok(utf8buf) => Ok(utf8buf),
        _ => bail!(
            "Error de codificación del archivo {}",
            path.as_ref().display()
        ),
    }
}

// Lee a una cadena un archivo en utf8
pub fn read_file<T: AsRef<Path>>(path: T) -> anyhow::Result<String> {
    let mut buf = String::new();
    BufReader::new(File::open(path.as_ref())?)
        .read_to_string(&mut buf)
        .with_context(|| {
            format!(
                "No se ha podido leer el archivo {}",
                path.as_ref().display()
            )
        })?;
    Ok(buf)
}

/// Redondea valor a 2 decimales
pub fn fround2(val: f32) -> f32 {
    (val * 100.0).round() / 100.0
}

/// Redondea valor a 3 decimales
pub fn fround3(val: f32) -> f32 {
    (val * 1000.0).round() / 1000.0
}

/// Normaliza número a un intervalo arbitrario (wrapping)
pub fn normalize(value: f32, start: f32, end: f32) -> f32 {
    // ancho del intervalo
    let width = end - start;
    // convertimos el intervalo a [0, ancho] restando el valor inicial
    let offset = value - start;
    // volvemos a sumar el valor incial para volver al intervalo [start, end]
    (offset - (f32::floor(offset / width) * width)) + start
}

/// Convierte ángulo (azimuth) desde el criterio del BDL al criterio de la 52016-1
/// BDL: Ángulo entre el eje Y del espacio y la proyección horizontal de la normal exterior del muro
/// UNE-EN ISO 52016-1: S=0, E=+90, W=-90
pub fn orientation_bdl_to_52016(azimuth: f32) -> f32 {
    normalize(180.0 - azimuth, -180.0, 180.0)
}

/// Nombre del ángulo a partir de su valor sexagesimal (0 -> 360)
/// El ángulo se define respecto al sur (sur = 0)
/// y crece en sentido antihorario, según DB-HE1 figura A.1
pub fn angle_name(angle: f32) -> String {
    let angle = normalize(angle, 0.0, 360.0);
    let name = if angle < 18.0 {
        "S"
    } else if angle < 69.0 {
        "SE"
    } else if angle < 120.0 {
        "E"
    } else if angle < 157.5 {
        "NE"
    } else if angle < 202.5 {
        "N"
    }
    // 202.5 = 360 - 157.5
    else if angle < 240.0 {
        "NW"
    }
    // 240 = 360 - 120
    else if angle < 291.0 {
        "W"
    }
    // 291 = 360 - 69
    else if angle < 342.0 {
        "SW"
    }
    // 342 = 360 - 18
    else {
        "S"
    };
    name.to_string()
}

/// Calcula UUID a partir de hash MD5 del objeto
///
/// Este no es un método muy robusto pero da valores estables para los mismos objetos
pub fn uuid_from_obj(obj: &impl std::fmt::Debug) -> String {
    let h = format!("{:x}", md5::compute(format!("{:?}", obj).as_bytes()));
    format!(
        "{}-{}-{}-{}-{}",
        &h[0..8],
        &h[8..12],
        &h[12..16],
        &h[16..20],
        &h[20..32]
    )
}